Thursday, April 10, 2008

String Manipulation In .NET

I've just realized a fact about string manipulation in .NET framework after read "MCTS Self-Paced Training Kit (Exam 70-536): Microsoft .NET Framework 2.0 Application Development Foundation" by Tony Northrup, Shawn Wildermuth and Bill Ryan.

For you who are an expert in .NET, this might look a common stuff. But, this is new to me. Well, I believe programming is not just about logic and algorithm, but also about knowing the boundaries. So you need to wide your boundaries if you want to solve various problem.

Back to the topic, the book said that
Strings of type System.String are immutable in .NET. That means any change to a string causes the runtime to create a new string and abandon the old one.
So when you want to construct a string as follows (this source is taken from the book too):

string s;

s = "wombat"; // "wombat"
s += " kangaroo"; // "wombat kangaroo"
s += " wallaby"; // "wombat kangaroo wallaby"
s += " koala"; // "wombat kangaroo wallaby koala"


In the end you'll get "wombat kangaroo wallaby koala" printed in the console, but in fact the compiler will create the "temporary strings" in memory before give the result string to you.

For a application which only a little string manipulation processes this will not give much trouble. But for application which use a bunch of string manipulations in it, this should be a concern because it can affect perfomance.

For the solution, the book suggested us to use StringBuilder. So, the code above will be as follow:

System.Text.StringBuilder sb = new System.Text.StringBuilder(30);

sb.Append("wombat"); // Build string.
sb.Append(" kangaroo");
sb.Append(" wallaby");
sb.Append(" koala");

string s = sb.ToString(); // Copy result to string.


No comments: