C
C#2w ago
Faker

✅ 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
FusedQyou
FusedQyou2w ago
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
Faker
FakerOP2w ago
yeah, everytime we allocate new memory
FusedQyou
FusedQyou2w ago
Stringbuilder avoids this by allowing you to build up strings without the whole additional allocation for each addition
Faker
FakerOP2w ago
yep I see, but what happens internally? it deletes the original string at a particular memory address then replace it by the new one ?
FusedQyou
FusedQyou2w ago
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 this
Faker
FakerOP2w ago
yep 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?
asdf
asdf2w ago
String is like char[] (but immutable). String builder is like List<char>
Faker
FakerOP2w ago
yeah I see this is where we need to use the ToString method I think ?
x0rld
x0rld2w ago
when you finished to build your string with the stringBuilder you do ToString and you have the total
FusedQyou
FusedQyou2w ago
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
Faker
FakerOP2w ago
yep I see, I haven't dive into spans yet but it seems an interesting topic, will do so later on, thanks !
Joschi
Joschi2w ago
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.
Faker
FakerOP7d ago
Oh interesting, yep I see, thanks !
canton7
canton77d ago
$close
MODiX
MODiX7d ago
If you have no further questions, please use /close to mark the forum thread as answered
canton7
canton77d ago
@Faker You're opening a lot of threads here, which is absolutely fine, but please remember to close them off when you're done!
Faker
FakerOP6d ago
yep, sorry

Did you find this page helpful?