C#的StringBuilder 类使用说明
一:StringBuilder 类简单说明
StringBuilder 类位于System.Text命名空间下,表示可变字符字符串, 此类不能被继承。使用StringBuilder类每次重新生成新字符串时不是再生成一个新实例,而是直接再原来字符串占用的内存看空间上进行处理,而且它可以动态的分配占用内存空间大小。因此在字符串处理操作比较多的情况下,使用StringBuilder类可以大大提高系统的性能。
简单来说,StringBuilder 在进行运算时,是一直在已有对象操作的,适合大量频繁字符串的拼接或删除。
二:StringBuilder 类的构造函数
构造函数 | 构造函数说明 |
---|---|
StringBuilder() | 初始化 StringBuilder 类的新实例。 |
StringBuilder(Int32) | 使用指定的容量 初始化 StringBuilder 类的新实例。 |
StringBuilder(Int32, Int32) | 初始化 StringBuilder 类的新实例,该类起始于指定容量并且可增长到指定的最大容量 。 |
StringBuilder(String) | 使用指定的字符串 初始化 StringBuilder 类的新实例。 |
StringBuilder(String, Int32) | 使用指定的字符串和容量初始化 StringBuilder 类的新实例。 |
StringBuilder(String, Int32, Int32, Int32) | 从指定的子字符串和容量初始化 StringBuilder 类的新实例。 |
三:StringBuilder 类的属性
构造函数 | 构造函数说明 |
---|---|
Capacity | 获取或设置可包含在当前实例所分配的内存中的最大字符数。 |
Chars[Int32] | 获取或设置此实例中指定字符位置处的字符。 |
Length | 获取或设置当前 StringBuilder 对象的长度。 |
MaxCapacity | 获取此实例的最大容量。 |
StringBuilder需要时动态分配更多的空间,并 Capacity 相应增加。 出于性能原因, StringBuilder 可能会分配比所需内存更多的内存。 分配的内存量是特定于实现的。 |
1: StringBuilder 类的属性使用举例
str = new StringBuilder("test string.");
Console.WriteLine("字符串是:{0},长度:{1}", str, str.Length);
Console.WriteLine("内存容量分配:{0}", str.Capacity);
Console.WriteLine("下标为3的字符是:{0}", str[3]);
运行结果:
字符串是:test string.,长度:12 内存容量分配:16 下标为3的字符是:t |
四:StringBuilder 类的常用方法
1: StringBuilder.Append 的常用方法
重载 | 重载说明 |
---|---|
Append(UInt32) | 向此字符串后面,追加指定的 32 位无符号整数的字符串 |
Append(Char, Int32) | 向此字符串后面,追加指定数量的字符 |
Append(Char[], Int32, Int32) | 向此字符串后面,追加指定字符数组的字符(从指定位置开始,取多少个) |
Append(String) | 向此字符串后面,追加另外一个字符串 |
Append(Char[]) | 向此字符串后面,追加字符数组 |
StringBuilder str = new StringBuilder();
str = new StringBuilder("Do one thing at a time, ");
str.Append("and do well.");
Console.WriteLine("字符串是:{0},长度:{1}", str, str.Length);
Console.WriteLine("内存容量分配:{0}", str.Capacity);
Console.WriteLine("拼接后的字符串是:{0}\r\n", str);
str.Append(666);//向此实例追加指定的32或者64位无符号整数的字符串表示形式。
Console.WriteLine("拼接后的字符串是:{0}\r\n", str);
str.Append('*',5);//向此字符串后面,追加指定数量的字符
Console.WriteLine("拼接后的字符串是:{0}\r\n", str);
char[] chars = { 'a', 'b', 'c', 'd', 'e', 'f' };
str.Append(chars);//向此字符串后面,追加整个字符数组
Console.WriteLine("拼接后的字符串是:{0}\r\n", str);
str.Append(chars,1,3);//向此字符串后面,追加指定字符数组的字符
Console.WriteLine("拼接后的字符串是:{0}\r\n", str);
运行结果:
字符串是:Do one thing at a time, and do well.,长度:36 内存容量分配:48 拼接后的字符串是:Do one thing at a time, and do well. 拼接后的字符串是:Do one thing at a time, and do well.666 拼接后的字符串是:Do one thing at a time, and do well.666***** 拼接后的字符串是:Do one thing at a time, and do well.666*****abcdef 拼接后的字符串是:Do one thing at a time, and do well.666*****abcdefbcd |
2: StringBuilder.AppendFormat 的常用方法
此方法使用 .NET Framework 的复合格式设置功能,将对象的值转换为其文本表示形式,并将该表示形式嵌入到当前 StringBuilder 对象中。
重载 | 重载说明 |
---|---|
AppendFormat(String, Object) | 追加复合格式字符串,然后返回的字符串。 每个格式项都替换为一个自变量的字符串表示形式。 |
AppendFormat(String, Object[]) | 追加复合格式字符串,然后返回的字符串。 每个格式项都由参数数组中相应参数的字符串表示形式替换。 |
AppendFormat(IFormatProvider, String, Object[]) | 追加复合格式字符串,然后返回的字符串。 每个格式项都使用指定的格式提供程序由参数数组中相应参数的字符串表示形式替换。 |
AppendFormat(String, Object, Object) | 追加复合格式字符串,然后返回的字符串。 每个格式项都替换为这两个参数中任意一个参数的字符串表示形式。 |
AppendFormat(String, Object, Object, Object) | 追加复合格式字符串,然后返回的字符串。 每个格式项都替换为这三个参数中任意一个参数的字符串表示形式。 |
StringBuilder str = new StringBuilder();
int var1 = 111;
float var2 = 2.22F;
string var3 = "and";
string var4 = "EF";
string var5 = "SD";
object[] var6 = { 520, 5.5, 'X' };
str = new StringBuilder("AB CD ");
str.AppendFormat("1) {0}", var1);
Console.WriteLine(str);
str.AppendFormat("2) {0}, {1}", var2, var3);
Console.WriteLine(str);
str.AppendFormat("3) {0}, {1}, {2}", var3 , var4, var5);
Console.WriteLine(str);
str.AppendFormat("4) {0}, {1}, {2}", var6);
Console.WriteLine(str);
运行结果:
AB CD 1) 111 AB CD 1) 1112) 2.22, and AB CD 1) 1112) 2.22, and3) and, EF, SD AB CD 1) 1112) 2.22, and3) and, EF, SD4) 520, 5.5, X |
3: StringBuilder.CopyTo 的常用方法
此方法就是,将源字符串中,指定下标开始,复制指定数量字符到目标字符数组里,并指明开始的位置。下面语法的参数sourceIndex是此实例中字符复制的起始位置。dest是要复制字符的数组,而destIndex是目标将复制字符的起始位置。count参数是要复制的字符数。
public void CopyTo (int sourceIndex, char[] destination, int destinationIndex, int count);
char[] dest = new char[6];
StringBuilder src = new StringBuilder("abcdefghijkl");
dest[1] = ')';
dest[2] = ' ';
// 将源复制到目标,分4个片段,每个片段3个字符。
Console.WriteLine("\nPiece) Data:");
for (int ix = 0; ix < 9; ix++)
{
dest[0] = ix.ToString()[0];
src.CopyTo(ix * 3, dest, 3, 3);
Console.Write(" ");
Console.WriteLine(dest);
}
运行结果:
Piece) Data: 0) abc 1) def 2) ghi 3) jkl |
4: StringBuilder.Insert 的常用方法
此方法就是,将指定对象的字符串,插入到此实例中的指定字符位置。
重载 | 重载说明 |
---|---|
Insert(Int32, UInt32) | 将 32 位无符号整数,变成字符串表示形式,插入到此实例中的指定字符位置。 |
Insert(Int32, String) | 在实例中的指定字符位置,插入字符串到此。 |
Insert(Int32, String, Int32) | 将指定字符串的一个或更多副本,插入到此实例中的指定字符位置。 |
Insert(Int32, Char) | 将指定的 Unicode 字符的字符串,变成字符串表示形式,插入到此实例中的指定位置。 |
Insert(Int32, Char[]) | 将指定的 Unicode 字符数组,变成字符串表示形式,插入到此实例中的指定字符位置。 |
Insert(Int32, Char[], Int32, Int32) | 将指定的 Unicode 字符子数组,变成字符串表示形式,插入到此实例中的指定字符位置。 |
Insert(Int32, Object) | 将对象的字符串表示形式,插入到此实例中的指定字符位置。 |
使用举例:
static string initialValue = "--[]--";
static StringBuilder sb;
static void Main(string[] args)
{
string xyz = "xyz";
char[] abc = { 'a', 'b', 'c' };
char star = '*';
Object obj = 0;
int xInt32 = 3;
sb = new StringBuilder(initialValue);
sb.Insert(3, xyz, 2); //Insert(Int32, String, Int32)
Show(1, sb);
sb.Insert(3, xyz); //Insert(Int32, String)
Show(2, sb);
sb.Insert(3, star); //Insert(Int32, Char)
Show(3, sb);
sb.Insert(3, abc); //Insert(Int32, Char[])
Show(4, sb);
sb.Insert(3, abc, 1, 2); //Insert(Int32, Char[], Int32, Int32)
Show(5, sb);
sb.Insert(3, obj); // Insert(Int32, Object)
Show(6, sb);
sb.Insert(3, xInt32); // 3
Show(7, sb);
}
public static void Show(int Number, StringBuilder sbs)
{
Console.WriteLine("{0,2:G} = {1}", Number, sbs.ToString());
sb = new StringBuilder(initialValue);//重新实例化对象;
}
运行结果:
1 = --[xyzxyz]-- 2 = --[xyz]-- 3 = --[*]-- 4 = --[abc]-- 5 = --[bc]-- 6 = --[0]-- 7 = --[3]-- |
5: StringBuilder.Remove(Int32, Int32) 的常用方法
此方法就是,将指定范围的字符从此实例中移除。
public System.Text.StringBuilder Remove (int startIndex, int length);
使用举例:
string str = "The quick brown fox jumps over the lazy dog.";
StringBuilder sb = new StringBuilder(str);
sb.Remove(10, 6); // Remove "brown "
Console.WriteLine("{0}", sb.ToString());
运行结果:
The quick fox jumps over the lazy dog. |
6: StringBuilder.Replace 方法
此方法就是,将此实例中出现的所有指定字符或字符串,替换为其他的指定字符或字符串。
重载 | 重载说明 |
---|---|
Replace(Char, Char) | 将此实例中出现的所有指定字符,替换为其他指定字符。 |
Replace(String, String) | 将此实例中出现的所有指定字符串,替换为其他指定字符串。 |
Replace(Char, Char, Int32, Int32) | 将此实例的子字符串中出现的所有指定字符,替换为其他指定字符。 |
Replace(String, String, Int32, Int32) | 将此实例的子字符串中出现的所有指定字符串,替换为其他指定字符串。 |
public System.Text.StringBuilder Replace (char oldChar, char newChar);
public System.Text.StringBuilder Replace (string oldValue, string newValue);
public System.Text.StringBuilder Replace (char oldChar, char newChar, int startIndex, int count);
public System.Text.StringBuilder Replace (string oldValue, string newValue, int startIndex, int count);
使用举例:
string str = "The quick br!wn d#g jumps #ver the lazy cat.";
StringBuilder sb = new StringBuilder(str);
sb.Replace('#', '!', 15, 29); // Some '#' -> '!'
Console.WriteLine(sb);
sb = new StringBuilder(str);
sb.Replace('!', 'o'); // All '!' -> 'o'
Console.WriteLine(sb);
sb = new StringBuilder(str);
sb.Replace("cat", "dog"); // All "cat" -> "dog"
Console.WriteLine(sb);
sb = new StringBuilder(str);
sb.Replace("dog", "fox", 15, 20); // Some "dog" -> "fox"
Console.WriteLine(sb);
运行结果:
The quick br!wn d!g jumps !ver the lazy cat. The quick brown d#g jumps #ver the lazy cat. The quick br!wn d#g jumps #ver the lazy dog. The quick br!wn d#g jumps #ver the lazy cat. |