/*The String object is immutable. Every time you use one of the methods in the System.String class, you create a new string object in memory, which requires a new allocation of space for that new object. In situations where you need to perform repeated modifications to a string, the overhead associated with creating a new String object can be costly. The System.Text.StringBuilder class can be used when you want to modify a string without creating a new object. For example, using the StringBuilder class can boost performance when concatenating many strings together in a loop.*/
using System;
using System.Text;
namespace StringBuilderExample
{
class Program
{
static void Main(string[] args)
{
//Append
//The Append method can be used to add text or a string representation
//of an object to the end of a string represented by the current StringBuilder.
StringBuilder strBuilder = new StringBuilder("Hello World!");
strBuilder.Append(" Appended.");
Console.WriteLine(strBuilder);
//--------------------------------------------
//AppendFormat
//The AppendFormat method adds text to the end of the StringBuilder,but also implements the IFormattable interface and therefore accepts the standard format strings described in the formatting section.
//You can use this method to customize the format of variables and append those values to a StringBuilder.
int amount = 25;
StringBuilder strBuilder2 = new StringBuilder("Your total is ");
strBuilder2.AppendFormat("{0:C} ", amount);
Console.WriteLine(strBuilder2);
//-----------------------------------
//Insert
//The Insert method adds a string or object to a specified position in the current StringBuilder.
StringBuilder strBuilder3 = new StringBuilder("Hello World!");
strBuilder3.Insert(6, "Beautiful ");
Console.WriteLine(strBuilder3);
//---------------------------------------------------
//Remove
//You can use the Remove method to remove a specified number of characters from the current StringBuilder, beginning at a specified zero-based index.
StringBuilder strBuilder4 = new StringBuilder("Hello World!");
strBuilder4.Remove(5, 7);
Console.WriteLine(strBuilder4);
//----------------------------------------------------
//Replace
//The Replace method can be used to replace characters within the StringBuilder object with another specified character.
StringBuilder strBuilder5 = new StringBuilder("Hello World!");
strBuilder5.Replace('!', '?');
Console.WriteLine(strBuilder5);
Console.Read();
}
}
}
NasserPosted Aug 27, 2011, 10:33 PM
very clear to me as beginner,thank you.. just want to ask about if we can use this class to replace words.. if No, pls. tell me what is the best class that replacement words in txt file ? (ex. 20 Mb of size)