✅ StringBuilder in C#
Hello guys, I was just reading a bit about string manipulation, then I came across the class "StringBuilder". I just read a bit about it. From what I've understood, it makes as if our string become "mutable" in the sense that we can directly modify our original string? (or it just shares the same memory location with another string?).
My question is, is StringBuilder Class still used? I mean like we have powerful methods for string now, like split, join, remove, substring etc. If so, can someone give any use case where we need to use StringBuilder please. (ALSO, please confirm whether I've said above about how StringBuilder works is true please)
17 Replies
Its very much used. The main issue with strings is that any "modification" like adding characters or splitting them makes new string instances and all these are allocated like classes.
Because they are immutable as you know. You make copies instead
yeah, everytime we allocate new memory
Stringbuilder avoids this by allowing you to build up strings without the whole additional allocation for each addition
yep I see, but what happens internally? it deletes the original string at a particular memory address then replace it by the new one ?
Im the end its a fancy wrapper I believe and it just uses characters which by itself do not allocate. That's also why you have to then
ToString
the result for the actual string
Let me check
https://referencesource.microsoft.com/#mscorlib/system/text/stringbuilder.cs,adf60ee46ebd299f
Im om phone so its hard to check
But you see it uses something called chunkchars that is an array of chars
So it still allocated, it just removes the constant allocation of new strings when you are appending parts
I think Spans kind of make this obsolete of you know how to use them. I personally like to stick to this though
Though Spans might nog be a full on replacement. Somebody else might be able to comment on thisyep I see
but hmm StringBuilder class replaces all methods of strings, like join/split etc? or only part of them
or can we use string methods on stringbuilder objects?
String is like char[] (but immutable). String builder is like List<char>
yeah I see
this is where we need to use the ToString method I think ?
when you finished to build your string with the stringBuilder you do ToString and you have the total
Yes
But this is also the charm with Spans because they have slicing and such too
And they dont allocate
So therefore idk how relevant stringbuilder are now
Spans also need to be turned back into strings because you can use them btw
yep I see, I haven't dive into spans yet but it seems an interesting topic, will do so later on, thanks !
StringBuilder is mostly used for string creation not manipulation.
For example you want to return a message to the user, but depending on the context you may add a lot of different lines.
Instead of directly merging the strings you would use a StringBuilder.
Oh interesting, yep I see, thanks !
$close
If you have no further questions, please use /close to mark the forum thread as answered
@Faker You're opening a lot of threads here, which is absolutely fine, but please remember to close them off when you're done!
yep, sorry