Monday, May 12, 2008

StringBuilder Class in .Net

StringBuilder class is mutable where as String Class is immutable. The value is said to be mutable because it can be modified once it has been created by appending, removing, replacing, or inserting characters.
StringBuilder class is well suited for String Manipulation tasks.

using System.Text;

private void btnString_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
sb.Append("Shalvin");
MessageBox.Show(sb.ToString());

sb.Insert(0, "Hello ");
MessageBox.Show(sb.ToString());

sb.Insert(6, "how are you ");
MessageBox.Show(sb.ToString());
sb.Remove(6, 8);
MessageBox.Show(sb.ToString());

sb.Replace("Shalvin", "Shalvin P D");
MessageBox.Show(sb.ToString());

MessageBox.Show(sb.Length.ToString());
}

No comments:

Post a Comment