学用 ASP.Net 之 "字符串" (5): StringBuilder 类
时间:2011-01-04 来源:i617
StringBuilder 的属性与方法:
/* 属性 */ Capacity; // 容量; 可读写, 也可在构建时设置, 但改写时使用 EnsureCapacity() 更保险 Length; // 长度; 可读写, 写入 0 时, 同 Clear(); Length <= Capacity <= MaxCapacity MaxCapacity; // 最大容量; 只读, 只能在构建时设置, 超出则异常 /* 方法 */ Append(); // 追加; 为接受不同类型的数据, 它有很多重载 AppendFormat(); // 按格式追加 AppendLine(); // 追加换行符 Clear(); // 取消; 之后 Length = 0; 但 Capacity、MaxCapacity 不变 CopyTo(); // 复制指定部分到 Char[] EnsureCapacity(); // 设置 Capacity Insert(); // 插入 Remove(); // 移除 Replace(); // 替换 ToString(); // 输出文本, 可同时截取
构造函数的 6 种重载:
using System.Text; // StringBuilder 所属的命名空间 protected void Button1_Click(object sender, EventArgs e) { string str = ""; /* 无参时, Capacity 默认 16 */ StringBuilder sb1 = new StringBuilder(); str += sb1.Capacity.ToString("Capacity: #\n"); //Capacity: 16 /* 构建时指定 Capacity 大小 */ StringBuilder sb2 = new StringBuilder(11); str += sb2.Capacity.ToString("Capacity: #\n"); //Capacity: 11 /* 使用字符串构建时, 如果字符串 Length > 16, 其 Capacity 同字符串的长度 */ StringBuilder sb3 = new StringBuilder("abcdefghijklmnopqrstuvwxyz"); str += sb3.Capacity.ToString("Capacity: #\n"); //Capacity: 26 /* 指定 Capacity 和 MaxCapacity */ StringBuilder sb4 = new StringBuilder(4, 10); str += sb4.Capacity.ToString("Capacity: #\t"); //Capacity: 4 sb4.Append("1234567890"); str += sb4.Capacity.ToString("Capacity: #\n"); //Capacity: 10 try { sb4.Append("ABC"); } catch (Exception err) { str += err.Message + "\n"; } //容量小于当前大小... /* 用字符串构建, 同时指定 Capacity */ StringBuilder sb5 = new StringBuilder("1234567890", 32); str += string.Format("Capacity: {0}\tLength: {1}\n", sb5.Capacity, sb5.Length); //Capacity: 32 Length: 10 /* 从字符串截取构建, 同时指定 Capacity */ StringBuilder sb6 = new StringBuilder("ABCDEFG", 1, 3, 12); str += string.Format("\"{0}\"\tCapacity: {1}\tLength: {2}", sb6, sb6.Capacity, sb6.Length); //"BCD" Capacity: 12 Length: 3 TextBox1.Text = str; }
练习:
//ToString() protected void Button1_Click(object sender, EventArgs e) { StringBuilder sb = new StringBuilder("1234567890"); string s1 = sb.ToString(); //1234567890 string s2 = sb.ToString(2, 3); //345 TextBox1.Text = s1 + "\n" + s2; } //Append()、AppendFormat()、AppendLine() protected void Button2_Click(object sender, EventArgs e) { StringBuilder sb = new StringBuilder(256); sb.Append("abcdefg"); sb.AppendLine(); sb.Append(3.1415926); sb.AppendLine(); sb.AppendFormat("{0:#.##}", 3.1415926); TextBox1.Text = sb.ToString(); /* abcdefg 3.1415926 3.14 */ } //Insert()、Remove()、Replace() protected void Button3_Click(object sender, EventArgs e) { StringBuilder sb = new StringBuilder("..........", 256); string s1 = sb.Insert(2, "ABC").ToString(); //..ABC........ string s2 = sb.Insert(2, 3.14).ToString(); //..3.14ABC........ string s3 = sb.Remove(6, 3).ToString(); //..3.14........ string s4 = sb.Replace('.', '*').ToString(); //**3*14******** TextBox1.Text = s1 + "\n" + s2 + "\n" + s3 + "\n" + s4; } //CopyTo protected void Button4_Click(object sender, EventArgs e) { string str = ".........."; char[] cArr = str.ToCharArray(); StringBuilder sb = new StringBuilder("1234567890"); sb.CopyTo(2, cArr, 0, 3); TextBox1.Text = new string(cArr); //345....... }
相关阅读 更多 +