1. Home
  2. Computing & Technology
  3. C / C++ / C#

C# Programming Tutorial - About Strings and Chars

By , About.com Guide

5 of 10

StringBuillder for Efficiently Building Strings

A StringBuilder object lets you build up a string in memory efficiently and when you are finished building it you can copy into a string. StringBuilder has methods to let you Append, Replace, Insert, and Remove characters from a StringBuilder String. You can also directly access and manipulate the chars in the object by indexing.
StringBuilder SB = "HEllo World";
SB[1]='e'; // Changes it to Hello World
Note: both the help with Visual C# 2008 Express Edition and the Microsoft website mention a Chars property of StringBuilder that you should use. That's confusing as it doesn't exist as the word Chars. You just use [] to access the characters in a StringBuilder class in the way I have shown. If you want to see what methods and properties are available in any class in the IDE, right click on the class name and click Go To Definition.

If you view the definition you'll see all 6 overloads for constructing a StringBuilder, Three properties (Capacity, Length and MaxCapacity), the default indexer (that is what makes this line work- SB[1]='e'; ), 19 overloads for Append etc, as well as Replace and Inserts.

This is a very handy class. Even the ToString() function is overloaded so you can just copy a number of characters from a starting position.

On the next page - Comparing Strings in C#

Explore C / C++ / C#
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. C / C++ / C#
  4. C# / C Sharp
  5. Learn C Sharp
  6. StringBuillder for Efficiently Building Strings

©2009 About.com, a part of The New York Times Company.

All rights reserved.