1、C# Programming in C# StringBuilder 类 C#程序设计 1 StringBuilder 2 1. StringBuilder 3 StringBuilder 类 表示可变字 符字符 串,因 为在通 过追加 、移除、替 换或插 入字符 而创建 它后可 以对它 进行修 改。 1. StringBuilder 4 StringBuilder 的 容 量 是实例 在任何 给定时 间可存 储的最 大字符 数 ,并且 大于或 等于实 例值的 字符串 表示形 式的长 度。 容量 可 通过 Capacity 属性或 EnsureCapacity 方 法 来 增加或 减少 , 但
2、它不 能小于 Length 属 性 的值。 如 果 在初始 化 StringBuilder 的 实 例 时没 有指定 容量或 最大容量, 则使用 特定于 实现的 默认值 。 1. StringBuilder 5 String 对 象 串 联操作 总是用 现有字 符串和 新数据 创建新 的对象。 StringBuilder 对 象 维 护一 个缓冲 区,以 便容纳 新数据 的串联。如 果有足 够的空 间,新 数据将 被追加 到缓冲 区的末 尾;否则, 将分配 一个新 的、更 大的缓 冲区, 原始缓 冲区中 的数据被复 制到新 的缓冲 区,然 后将新 数据追 加到新 的缓冲 区。 1. Strin
3、gBuilder 6 String 或 StringBuilder 对 象 的 串联 操作的 性能取 决于内 存分配的发 生频率 。 String 串 联 操 作每次 都分配 内存, 而 StringBuilder 串联操作仅当 StringBuilder 对象 缓冲区太小 而 无法容 纳新数 据时才分 配 内存。 如果 串联固 定数量 的 String 对象,则 String 类更 适合串联操 作 ;如果 串联任 意数量 的字符 串,则 StringBuilder 对象更适合串 联操作 。 2.StringBulider 7 StringBulider 类 的属性有四 个: (1 ) Cha
4、rs : 获 取 或设置 当前 StringBulider 对 象 中位于指定字符位 置的字 符。 (2 )Length : 获 取 或 设置当 前 StringBulider 对 象 的 长度 。 属性的 使用实 例如下 : StringBuilder s = new StringBuilder(Hello world!); Console.WriteLine(s0);/ 获 取 索引位 置 为0 的char对象, 输 出H Console.WriteLine(s.Length);/输出12 2.StringBulider 8 (3 ) Capacity : 获 取或设置可 包含在 当前实
5、例所分 配的内 存中的最 大字符 数。 (4 ) MaxCapacity : 获取此实例 的最大 容量。 属性的 使用实 例如下 : StringBuilder s = new StringBuilder(Hello world!); Console.WriteLine(s.Capacity);/输出16 Console.WriteLine(s.MaxCapacity);/输出2147483647 3.StringBulider 9 (1 ) Append 方法 具有多 种重载 形式, 实现在 此实例 的结尾 追加指 定数据 类型值的字 符串表 示形式 。 3.StringBulider 10
6、 下面是Append 方法的 使用实 例。 StringBuilder s = new StringBuilder(Hello world!); Console.WriteLine(s.Append(true);/输出Hello world!True Console.WriteLine(s.Append(123);/输出Hello world!True123 Console.WriteLine(s.Append(C#);/输出Hello world!True123C# Console.WriteLine(s.Append(!,5);/输出Hello world!True123C#! char
7、temp=new char ,.; Console.WriteLine(s.Append(temp);/输出Hello world!True123C#! . Console.WriteLine(s.Append(aaa,0,3);/ 输出Hello world!True123C#! .aaa 3.StringBulider 11 下面是Append 方法的 使用实 例。 StringBuilder s = new StringBuilder(Hello world!); Console.WriteLine(s.Append(true);/输出Hello world!True Console.W
8、riteLine(s.Append(123);/输出Hello world!True123 Console.WriteLine(s.Append(C#);/输出Hello world!True123C# Console.WriteLine(s.Append(!,5);/输出Hello world!True123C#! char temp=new char ,.; Console.WriteLine(s.Append(temp);/输出Hello world!True123C#! . Console.WriteLine(s.Append(aaa,0,3);/ 输出Hello world!True
9、123C#! .aaa 3.StringBulider 12 下面是Append 方法的 使用实 例。 StringBuilder s = new StringBuilder(Hello world!); Console.WriteLine(s.Append(true);/输出Hello world!True Console.WriteLine(s.Append(123);/输出Hello world!True123 Console.WriteLine(s.Append(C#);/输出Hello world!True123C# Console.WriteLine(s.Append(!,5);/
10、输出Hello world!True123C#! char temp=new char ,.; Console.WriteLine(s.Append(temp);/输出Hello world!True123C#! . Console.WriteLine(s.Append(aaa,0,3);/ 输出Hello world!True123C#! .aaa 3.StringBulider 13 下面是Append 方法的 使用实 例。 StringBuilder s = new StringBuilder(Hello world!); Console.WriteLine(s.Append(true)
11、;/输出Hello world!True Console.WriteLine(s.Append(123);/输出Hello world!True123 Console.WriteLine(s.Append(C#);/输出Hello world!True123C# Console.WriteLine(s.Append(!,5);/输出Hello world!True123C#! char temp=new char ,.; Console.WriteLine(s.Append(temp);/输出Hello world!True123C#! . Console.WriteLine(s.Append
12、(aaa,0,3);/ 输出Hello world!True123C#! .aaa 3.StringBulider 14 下面是Append 方法的 使用实 例。 StringBuilder s = new StringBuilder(Hello world!); Console.WriteLine(s.Append(true);/输出Hello world!True Console.WriteLine(s.Append(123);/输出Hello world!True123 Console.WriteLine(s.Append(C#);/输出Hello world!True123C# Con
13、sole.WriteLine(s.Append(!,5);/输出Hello world!True123C#! char temp=new char ,.; Console.WriteLine(s.Append(temp);/输出Hello world!True123C#! . Console.WriteLine(s.Append(aaa,0,3);/ 输出Hello world!True123C#! .aaa 3.StringBulider 15 下面是Append 方法的 使用实 例。 StringBuilder s = new StringBuilder(Hello world!); Co
14、nsole.WriteLine(s.Append(true);/输出Hello world!True Console.WriteLine(s.Append(123);/输出Hello world!True123 Console.WriteLine(s.Append(C#);/输出Hello world!True123C# Console.WriteLine(s.Append(!,5);/输出Hello world!True123C#! char temp=new char ,.; Console.WriteLine(s.Append(temp);/输出Hello world!True123C#
15、! . Console.WriteLine(s.Append(aaa,0,3);/ 输出Hello world!True123C#! .aaa 3.StringBulider 16 (2 ) Clear 方法 从当前 StringBuilder 实 例 中 移除 所有字 符。 3.StringBulider 17 下面是Clear 方法 的使用 实例。 StringBuilder sb = new StringBuilder(This is a string.); Console.WriteLine(0 (1 characters), sb.ToString(), sb.Length);/输出
16、This is a string. (17 characters) sb.Clear(); Console.WriteLine(0 (1 characters), sb.ToString(), sb.Length);/输出 (0 characters) 3.StringBulider 18 (3 ) Equals 方法 返回一 个值, 该值指 示此实 例是否 与指定 的对象 相等。 3.StringBulider 19 下面是Equals 方法的 使用实 例。 StringBuilder sb1 = new StringBuilder(abc); StringBuilder sb2 = new
17、 StringBuilder(abc, 16); Console.WriteLine(a4) sb1 equals sb2: 0, sb1.Equals(sb2); 3.StringBulider 20 (4 ) Insert 方法 具有多 种重载 方式, 实现将 各种类 型值的 字符串 表示形 式插入到字 符串中 的指定 字符位 置。 3.StringBulider 21 下面是Insert 方法的使用 实例。 string xyz = xyz; char abc = a, b, c ; char star = *; Object obj = 0; bool xBool = true; by
18、te xByte = 1; short xInt16 = 2; int xInt32 = 3; long xInt64 = 4; Decimal xDecimal = 5; float xSingle = 6.6F; double xDouble = 7.7; StringBuilder sb = new StringBuilder(-); 3.StringBulider 22 下面是Insert 方法的使用 实例。 sb.Insert(3, xyz, 2); Console.WriteLine(sb);/输出-xyzxyz- sb.Insert(3, xyz); Console.WriteL
19、ine(sb);/输出-xyzxyzxyz- sb.Insert(3, star); Console.WriteLine(sb);/输出-*xyzxyzxyz- sb.Insert(3, abc); Console.WriteLine(sb);/输出-abc*xyzxyzxyz- sb.Insert(3, abc, 1, 2); Console.WriteLine(sb);/输出-bcabc*xyzxyzxyz- sb.Insert(3, xBool); Console.WriteLine(sb);/输出-Truebcabc*xyzxyzxyz- sb.Insert(3, obj);- 3.S
20、tringBulider 23 下面是Insert 方法的使用 实例。 sb.Insert(3, xyz, 2); Console.WriteLine(sb);/输出-xyzxyz- sb.Insert(3, xyz); Console.WriteLine(sb);/输出-xyzxyzxyz- sb.Insert(3, star); Console.WriteLine(sb);/输出-*xyzxyzxyz- sb.Insert(3, abc); Console.WriteLine(sb);/输出-abc*xyzxyzxyz- sb.Insert(3, abc, 1, 2); Console.W
21、riteLine(sb);/输出-bcabc*xyzxyzxyz- sb.Insert(3, xBool); Console.WriteLine(sb);/输出-Truebcabc*xyzxyzxyz- sb.Insert(3, obj);- 3.StringBulider 24 下面是Insert 方法的使用 实例。 sb.Insert(3, xyz, 2); Console.WriteLine(sb);/输出-xyzxyz- sb.Insert(3, xyz); Console.WriteLine(sb);/输出-xyzxyzxyz- sb.Insert(3, star); Console
22、.WriteLine(sb);/输出-*xyzxyzxyz- sb.Insert(3, abc); Console.WriteLine(sb);/输出-abc*xyzxyzxyz- sb.Insert(3, abc, 1, 2); Console.WriteLine(sb);/输出-bcabc*xyzxyzxyz- sb.Insert(3, xBool); Console.WriteLine(sb);/输出-Truebcabc*xyzxyzxyz- sb.Insert(3, obj);- 3.StringBulider 25 下面是Insert 方法的使用 实例。 sb.Insert(3, x
23、yz, 2); Console.WriteLine(sb);/输出-xyzxyz- sb.Insert(3, xyz); Console.WriteLine(sb);/输出-xyzxyzxyz- sb.Insert(3, star); Console.WriteLine(sb);/输出-*xyzxyzxyz- sb.Insert(3, abc); Console.WriteLine(sb);/输出-abc*xyzxyzxyz- sb.Insert(3, abc, 1, 2); Console.WriteLine(sb);/输出-bcabc*xyzxyzxyz- sb.Insert(3, xBo
24、ol); Console.WriteLine(sb);/输出-Truebcabc*xyzxyzxyz- sb.Insert(3, obj);- 3.StringBulider 26 下面是Insert 方法的使用 实例。 sb.Insert(3, xyz, 2); Console.WriteLine(sb);/输出-xyzxyz- sb.Insert(3, xyz); Console.WriteLine(sb);/输出-xyzxyzxyz- sb.Insert(3, star); Console.WriteLine(sb);/输出-*xyzxyzxyz- sb.Insert(3, abc);
25、Console.WriteLine(sb);/输出-abc*xyzxyzxyz- sb.Insert(3, abc, 1, 2); Console.WriteLine(sb);/输出-bcabc*xyzxyzxyz- sb.Insert(3, xBool); Console.WriteLine(sb);/输出-Truebcabc*xyzxyzxyz- sb.Insert(3, obj);- 3.StringBulider 27 下面是Insert 方法的使用 实例。 sb.Insert(3, xyz, 2); Console.WriteLine(sb);/输出-xyzxyz- sb.Inser
26、t(3, xyz); Console.WriteLine(sb);/输出-xyzxyzxyz- sb.Insert(3, star); Console.WriteLine(sb);/输出-*xyzxyzxyz- sb.Insert(3, abc); Console.WriteLine(sb);/输出-abc*xyzxyzxyz- sb.Insert(3, abc, 1, 2); Console.WriteLine(sb);/输出-bcabc*xyzxyzxyz- sb.Insert(3, xBool); Console.WriteLine(sb);/输出-Truebcabc*xyzxyzxyz
27、- sb.Insert(3, obj);- 3.StringBulider 28 下面是Insert 方法的使用 实例。 sb.Insert(3, xyz, 2); Console.WriteLine(sb);/输出-xyzxyz- sb.Insert(3, xyz); Console.WriteLine(sb);/输出-xyzxyzxyz- sb.Insert(3, star); Console.WriteLine(sb);/输出-*xyzxyzxyz- sb.Insert(3, abc); Console.WriteLine(sb);/输出-abc*xyzxyzxyz- sb.Insert
28、(3, abc, 1, 2); Console.WriteLine(sb);/输出-bcabc*xyzxyzxyz- sb.Insert(3, xBool); Console.WriteLine(sb);/输出-Truebcabc*xyzxyzxyz- sb.Insert(3, obj); 3.StringBulider 29 下面是Insert 方法的使用 实例。 Console.WriteLine(sb);/输出-0Truebcabc*xyzxyzxyz- sb.Insert(3, xByte); Console.WriteLine(sb);/输出-10Truebcabc*xyzxyzxy
29、z- sb.Insert(3, xInt16); Console.WriteLine(sb);/输出-210Truebcabc*xyzxyzxyz- sb.Insert(3, xInt32); Console.WriteLine(sb);/输出-3210Truebcabc*xyzxyzxyz- sb.Insert(3, xInt64); Console.WriteLine(sb);/输出-43210Truebcabc*xyzxyzxyz- sb.Insert(3, xDecimal); Console.WriteLine(sb);/输出-543210Truebcabc*xyzxyzxyz- 3
30、.StringBulider 30 下面是Insert 方法的使用 实例。 Console.WriteLine(sb);/输出-0Truebcabc*xyzxyzxyz- sb.Insert(3, xByte); Console.WriteLine(sb);/输出-10Truebcabc*xyzxyzxyz- sb.Insert(3, xInt16); Console.WriteLine(sb);/输出-210Truebcabc*xyzxyzxyz- sb.Insert(3, xInt32); Console.WriteLine(sb);/输出-3210Truebcabc*xyzxyzxyz-
31、 sb.Insert(3, xInt64); Console.WriteLine(sb);/输出-43210Truebcabc*xyzxyzxyz- sb.Insert(3, xDecimal); Console.WriteLine(sb);/输出-543210Truebcabc*xyzxyzxyz- 3.StringBulider 31 下面是Insert 方法的使用 实例。 Console.WriteLine(sb);/输出-0Truebcabc*xyzxyzxyz- sb.Insert(3, xByte); Console.WriteLine(sb);/输出-10Truebcabc*xy
32、zxyzxyz- sb.Insert(3, xInt16); Console.WriteLine(sb);/输出-210Truebcabc*xyzxyzxyz- sb.Insert(3, xInt32); Console.WriteLine(sb);/输出-3210Truebcabc*xyzxyzxyz- sb.Insert(3, xInt64); Console.WriteLine(sb);/输出-43210Truebcabc*xyzxyzxyz- sb.Insert(3, xDecimal); Console.WriteLine(sb);/输出-543210Truebcabc*xyzxyz
33、xyz- 3.StringBulider 32 下面是Insert 方法的使用 实例。 Console.WriteLine(sb);/输出-0Truebcabc*xyzxyzxyz- sb.Insert(3, xByte); Console.WriteLine(sb);/输出-10Truebcabc*xyzxyzxyz- sb.Insert(3, xInt16); Console.WriteLine(sb);/输出-210Truebcabc*xyzxyzxyz- sb.Insert(3, xInt32); Console.WriteLine(sb);/输出-3210Truebcabc*xyzx
34、yzxyz- sb.Insert(3, xInt64); Console.WriteLine(sb);/输出-43210Truebcabc*xyzxyzxyz- sb.Insert(3, xDecimal); Console.WriteLine(sb);/输出-543210Truebcabc*xyzxyzxyz- 3.StringBulider 33 下面是Insert 方法的使用 实例。 Console.WriteLine(sb);/输出-0Truebcabc*xyzxyzxyz- sb.Insert(3, xByte); Console.WriteLine(sb);/输出-10Truebc
35、abc*xyzxyzxyz- sb.Insert(3, xInt16); Console.WriteLine(sb);/输出-210Truebcabc*xyzxyzxyz- sb.Insert(3, xInt32); Console.WriteLine(sb);/输出-3210Truebcabc*xyzxyzxyz- sb.Insert(3, xInt64); Console.WriteLine(sb);/输出-43210Truebcabc*xyzxyzxyz- sb.Insert(3, xDecimal); Console.WriteLine(sb);/输出-543210Truebcabc*
36、xyzxyzxyz- 3.StringBulider 34 下面是Insert 方法的使用 实例。 sb.Insert(3, xSingle); Console.WriteLine(sb);/输出-6.6543210Truebcabc*xyzxyzxyz- sb.Insert(3, xDouble); Console.WriteLine(sb);/输出-7.76.6543210Truebcabc*xyzxyzxyz- 3.StringBulider 35 下面是Insert 方法的使用 实例。 sb.Insert(3, xSingle); Console.WriteLine(sb);/输出-6
37、.6543210Truebcabc*xyzxyzxyz- sb.Insert(3, xDouble); Console.WriteLine(sb);/输出-7.76.6543210Truebcabc*xyzxyzxyz- 3.StringBulider 36 (5 ) Remove 方法 将指定 范围的 字符从 此实例 中移除 。 3.StringBulider 37 下面是Remove 方法 的使用 实例。 string str = The quick brown fox jumps over the lazy dog.; StringBuilder sb = new StringBuild
38、er(str); sb.Remove(10, 6); / Remove brown Console.WriteLine(sb);/输出The quick fox jumps over the lazy dog. 3.StringBulider 38 (6 ) Replace 方法 将此实 例中所 有的指 定字符/ 子串替 换为其 他指定 字符/ 子串。 3.StringBulider 39 下面是Replace 方法的 使用实 例。 string str = The quick br!wn d#g jumps #ver the lazy cat.; StringBuilder sb = new
39、 StringBuilder(str); sb.Replace(#, !, 15, 29); / Some # - ! Console.WriteLine(sb);/输出The quick br!wn d!g jumps !ver the lazy cat. sb.Replace(!, o); / All ! - o Console.WriteLine(sb);/输出The quick brown dog jumps over the lazy cat. sb.Replace(cat, dog); / All cat - dog Console.WriteLine(sb);/输出The qui
40、ck brown dog jumps over the lazy dog. 3.StringBulider 40 下面是Replace 方法的 使用实 例。 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);/输出The quick br!wn d!g jumps !ver the lazy cat. sb.Replace
41、(!, o); / All ! - o Console.WriteLine(sb);/输出The quick brown dog jumps over the lazy cat. sb.Replace(cat, dog); / All cat - dog Console.WriteLine(sb);/输出The quick brown dog jumps over the lazy dog. 3.StringBulider 41 下面是Replace 方法的 使用实 例。 string str = The quick br!wn d#g jumps #ver the lazy cat.; St
42、ringBuilder sb = new StringBuilder(str); sb.Replace(#, !, 15, 29); / Some # - ! Console.WriteLine(sb);/输出The quick br!wn d!g jumps !ver the lazy cat. sb.Replace(!, o); / All ! - o Console.WriteLine(sb);/输出The quick brown dog jumps over the lazy cat. sb.Replace(cat, dog); / All cat - dog Console.Writ
43、eLine(sb);/输出The quick brown dog jumps over the lazy dog. 3.StringBulider 42 sb.Replace(dog, fox, 15, 20); / Some dog - fox Console.WriteLine(sb);/输出The quick brown fox jumps over the lazy dog. 下面是Replace 方法的 使用实 例。 3.StringBulider 43 (7 ) ToString 方法 将 此 实例的 值转换 为 String 。 3.StringBulider 44 下面是ToString 方法的使 用实例 。 string str = The quick br!wn d#g jumps #ver the lazy cat.; StringBuilder sb = new StringBuilder(str); Console.WriteLine(0 chars: 1, sb.Length, sb.ToString();/ 输出44 chars: The quick br!wn d#g jumps #ver the lazy cat. 结束