OOPS in Java. Constructors in Java. Interfaces in Java. Keywords in Java. Exception Handling in Java. Collection Framework. Multi-threading in Java. Save Article.
Improve Article. Like Article. StringBuilder str. StringBuilder str1. StringBuilder str2. StringBuilder str3. Previous StringBuffer class in Java. Recommended Articles. Article Contributed By :. Easy Normal Medium Hard Expert.
Writing code in comment? Please use ide. Load Comments. If you have an array of things to concatenate, consider calling String. Concat explicitly - or String.
Join if you need a delimiter. Don't be afraid to break literals up into several concatenated bits - the result will be the same. You can aid readability by breaking a long literal into several lines, for instance, with no harm to performance. If you need the intermediate results of the concatenation for something other than feeding the next iteration of concatenation, StringBuilder isn't going to help you. If you just have a few concatenations to do, and you really want to do them in separate statements, it doesn't really matter which way you go.
Which way is more efficient will depend on the number of concatenations the sizes of string involved, and what order they're concatenated in.
If you really believe that piece of code to be a performance bottleneck, profile or benchmark it both ways. String is an immutable object - it means that whenever you modify its content it will allocate a new string and this takes time and memory?
Using StringBuilder you modify the actual content of the object without allocating a new one. Not really Then shalt thou count to three, no more, no less. Three shall be the number thou shalt count, and the number of the counting shall be three. Four shalt thou not count, neither count thou two, excepting that thou then proceed to three.
Once the number three, being the third number, be reached, then lobbest thou thy Holy Hand Grenade of Antioch. I generally use string builder for any block of code which would result in the concatenation of three or more strings. But if I want to concatenate 2 strings, then I assume that it's better and faster to do so without StringBuilder. But more importantly, it is vastly more readable to use a vanilla String in such situations.
Using it in a loop, on the other hand, makes sense and can also be as readable as concatenation. Using it in loops and loops only is probably just as useful, easier to remember and makes more sense. Since it's difficult to find an explanation for this that's not either influenced by opinions or followed by a battle of prides I thought to write a bit of code on LINQpad to test this myself. I found that using small sized strings rather than using i.
ToString changes response times visible in small loops. The test uses different sequences of iterations to keep time measurements in sensibly comparable ranges. I'll copy the code at the end so you can try it yourself results. Iterations sequence: 2, 3, 4, 5, 6, 7, 8, 9, Iterations sequence: 10, 20, 30, 40, 50, 60, 70, Iterations sequence: , , , , The big problem is when you are concatenating hundreds of strings.
Which is pretty bad. I don't think there's a fine line between when to use or when not to. Unless of course someone performed some extensive testings to come out with the golden conditions.
For me, I will not use StringBuilder if just concatenating 2 huge strings. If there's loop with an undeterministic count, I'm likely to, even if the loop might be small counts. A single concatenation is not worth using a StringBuilder. I've typically used 5 concatenations as a rule of thumb. Stack Overflow for Teams — Collaborate and share knowledge with a private group.
Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. When to use StringBuilder? Ask Question. Asked 11 years, 11 months ago. The String object is immutable. Every time you use one of the methods in the System. String class, you create a new string object in memory, which requires a new allocation of space for that new object. In situations where you need to perform repeated modifications to a string, the overhead associated with creating a new String object can be costly.
The System. StringBuilder class can be used when you want to modify a string without creating a new object. For example, using the StringBuilder class can boost performance when concatenating many strings together in a loop. The StringBuilder class is found in the System. Text namespace. To avoid having to provide a fully qualified type name in your code, you can import the System.
Text namespace:. You can create a new instance of the StringBuilder class by initializing your variable with one of the overloaded constructor methods, as illustrated in the following example.
Although the StringBuilder is a dynamic object that allows you to expand the number of characters in the string that it encapsulates, you can specify a value for the maximum number of characters that it can hold. This value is called the capacity of the object and should not be confused with the length of the string that the current StringBuilder holds. For example, you might create a new instance of the StringBuilder class with the string "Hello", which has a length of 5, and you might specify that the object has a maximum capacity of When you modify the StringBuilder , it does not reallocate size for itself until the capacity is reached.
When this occurs, the new space is allocated automatically and the capacity is doubled. You can specify the capacity of the StringBuilder class using one of the overloaded constructors. The following example specifies that the myStringBuilder object can be expanded to a maximum of 25 spaces. The following example uses the Capacity property to define the maximum object length.
The EnsureCapacity method can be used to check the capacity of the current StringBuilder.
0コメント