Default capacity of StringBuilder
The default capacity of StringBuilder is 16 characters (I used .NET Reflector to find out).
The default capacity of StringBuilder is 16 characters (I used .NET Reflector to find out).
The StringBuilder class has a special version of the ToString method that takes two arguments, exactly as Substring(startIndex, length). StringBuilder sb = new StringBuilder(“This is a Test”); string test = sb.ToString(10, 4); Console.WriteLine(test); // result = Test
As you mention, you should not use StringBuilder instead of a simple string concatenation expression such as a + ” = ” + b. The latter is faster to type, easier to read, and the compiler will use a StringBuilder internally anyway so there is no performance advantage by rewriting it. However StringBuilder is useful …
If you also want to remove lines that only contain whitespace, use resultString = Regex.Replace(subjectString, @”^\s+$[\r\n]*”, string.Empty, RegexOptions.Multiline); ^\s+$ will remove everything from the first blank line to the last (in a contiguous block of empty lines), including lines that only contain tabs or spaces. [\r\n]* will then remove the last CRLF (or just LF …
Would MemoryStream work for you? The interface might not be quite as convenient, but it offers a simple way to append bytes, and when you are done you can get the contents as a byte[] by calling ToArray(). A more StringBuilder-like interface could probably be achieved by making an extension method. Update Extension methods could …
I know the question is old and has been marked as answered, but I thought I’d add this here in case anyone else comes across this as it’s the first hit on Google for StringBuilder.AppendLine() not working. I had the same problem and it turned out to be an Outlook issue. Outlook re-formats text based …
It’s impossible to say, not knowing the size of string1 and string2. With the call to AppendFormat, it will preallocate the buffer just once given the length of the format string and the strings that will be inserted and then concatenate everything and insert it into the buffer. For very large strings, this will be …
TagBuilder is a class that specially designed for creating html tags and their content. You are right saying that result will be anyway a string and of course you still can use StringBuilder and the result will be the same, but you can do things easier with TagBuilder. Lets say you need to generate a …
You can do a check on the object before appending it: sb.append(“Value: “); if (s != null) sb.append(s); System.out.println(sb); A key point to make is that null is not the same an an empty String. An empty String is still a String object with associated methods and fields associated with it, where a null pointer …
What is “better” solely depends on your requirements: For instance String Builder will be faster, but the code will be much more unreadable, and and it would be easier to make a mistake. On the other hand String.format() produces more readable code at the cost of performance. JMH benchmark to illustrate the performance difference (notice …