Understanding the StringBuilder Class
As I mentioned earlier in this article, instances of the StringBuilder class—as opposed to the String
class—are mutable, or dynamically changeable. StringBuilder is located in the System.Text namespace.
As you can see in the table, it does not have nearly as many members as the String class—but it does
have enough to get most jobs done. If you have a situation in which you need to perform many string
operations—for example, within a large loop—from a performance viewpoint it probably makes sense to
use StringBuilder instances instead of String instances.
Table: Key Instance Members of the StringBuilder Class
|
Member
|
What It Does
|
|
Append
|
Method adds string information to the end of the current StringBuilder instance. Overloads make for some
flexibility regarding the kinds of objects that can be appended (if not
already string, then the method converts the object to be appended to a
string representation).
|
|
AppendFormat
|
Method appends a formatted string to the current instance
(see “Formatting Overview” in online help for more information about format
specifications).
|
|
Capacity
|
Property sets or retrieves the maximum number of
characters that can be stored in the memory allocated for the StringBuilder instance.
|
|
Insert
|
Method inserts a string, or string representation of an
object, into the current StringBuilder instance at
the specified position.
|
|
Length
|
Property gets or sets the length of the instance. Setting
this to a value that is less than the length of the current instance
truncates the instance.
|
|
Remove
|
Method removes a specified number of characters from the
current StringBuilder instance.
|
|
Replace
|
Method replaces all occurrences of a specified character
in the current instance (or part of the current instance) with a specified
string.
|
There are six different overloads of the StringBuilder constructor, designed so
that you can create an instance already containing text and—if desired—set the
Length and Capacity properties. As you’d suspect, the shortest StringBuilder
constructor simply creates an instance without storing any text in it. The
listing demonstrates creating a StringBuilder instance on the fly. Next, the
Append method is used to store the contents of a TextBox in the StringBuilder.
The Length property is used to truncate the StringBuilder to four characters.
Finally, the StringBuilder is converted to a just plain vanilla string and
displayed in a message box.
Listing: Creating and Truncating a StringBuilder on the Fly
private void btnSB_Click(object sender,
System.EventArgs e) {
System.Text.StringBuilder theSB =
 new System.Text.StringBuilder();
theSB.Append (txtSB1.Text);
theSB.Length = 4;
MessageBox.Show (theSB.ToString(), "StringBuilder",
 MessageBoxButtons.OK, MessageBoxIcon.Information);
}
If you run the code shown in the listing, first adding some text to the TextBox, you’ll
see that the text has been appended to the StringBuilder, which is truncated at four characters.
Let’s do another StringBuilder example. The next listing appends the contents of three TextBoxes into
one StringBuilder. The user then enters two characters in a fourth TextBox. All instances of the first
character are replaced in the StringBuilder with the second character, and the StringBuilder is then
displayed in a multiline TextBox.
Listing: Appending and Replacing in a StringBuilder
private void btnSB_Click(object sender,
System.EventArgs e) {
System.Text.StringBuilder theSB =
 new System.Text.StringBuilder();
theSB.Append (txtSB1.Text);
theSB.Append (txtSB2.Text);
theSB.Append (txtSB3.Text);
txtDoIt.Text = "";
string str = txtReplace.Text;
theSB.Replace (str[0],str[1]);
txtDoIt.Text = theSB.ToString();
}
The result of running this code, entering the strings “A nose ”, “is a nose ”, and “is a nose”
in the TextBoxes, and replacing the character “n” with “r”, is shown in the figure.
Part I of this course explained the basic building blocks of C#, variables, types, and so on.
Part II explained arrays. This section explained strings—and, if you know how to work with strings,
you know a lot! If you’ve read through the three parts of this article, you don’t count as a
“newbie” any more. It’s time to use C# in your projects to “live long and prosper!”
Now that you've mastered the basic building blocks of the C# language, getting a clear notion of how events work in C# would be a very good thing.
|
|
Search Engine Optimization
 
Syndication Viewer
Our Web host:
IX WebHosting
|