Click here to Skip to main content
15,881,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Quick Question.

I am going through the article for String vs StringBuilder. I have couple of doubts

1.Which is better to use ( String or StringBuilder ) for strings that change constantly. Please explain why ( I think it is String. Correct me if I am wrong )
2. For large strings which is a good option ( I think it is StringBuilder )
3.Is string type optimized to accept changes without much impact ?
Posted

1 solution

The following article will clear your doubts:
String Vs StringBuilder (C#)[^]
 
Share this answer
 
Comments
Member 10491035 28-Dec-13 1:06am    
I went through the article. That clarifies the first question of mine. if you try to do some other manipulation (like removing a part from the string, replacing a part in the string, etc.), then it's better not to use StringBuilder at those places. This is because we are anyway creating new strings. So that means for strings that change constantly it is better to use String instead of StringBuilder.

I couldnt find anything which clarifies my Questions 2 and 3
Peter Leow 28-Dec-13 1:51am    
Question 2: I do not think the size of a string matters.
Question 3: A string is immutable: it cannot be changed. A new string is always created for concaternation,replace etc. So there is no question of optimization here.
Ultimately, it all falls back to question 1, to help you with more in-dept understanding, check these out
stringbuilder
performance
string-replace-vs-stringbuilder-replace
Member 10491035 28-Dec-13 12:41pm    
StringBuilder is used for strings that change constantly. - False
StringBuilder should be used only with big enough strings - True
String type is optimized to accommodate changes without much impact. --> False ( As we are creating new string everytime for every change i think its not optimized )
Let me know if my understanding is correct
Member 10491035 28-Dec-13 14:26pm    
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900