StringBuilder 类的使用
属性:
namespace StringBuilderTest { class Program { static void Main(string[] args) { StringBuilder s = new StringBuilder("hello,world!"); Console.WriteLine(s); //Length属性 Console.WriteLine("s.Length={0}", s.Length); //Chars属性 for (int i = 0; i < s.Length; i++) Console.Write(s[i] + " "); Console.WriteLine(); //Capacity属性 Console.WriteLine(s.Capacity); } } }
结果:
方法:
namespace StringBuilderTest { class Program { static void Main(string[] args) { //方法 //1: append方法 StringBuilder s = new StringBuilder("Welcome to "); s.Append("TangPro.com"); Console.WriteLine(s); // Insert方法 StringBuilder s1 = new StringBuilder("Hello world!"); s1.Insert(5, '-'); Console.WriteLine(s1); //Replace StringBuilder s2 = new StringBuilder("Hello world"); s2.Replace("world", "Tang"); Console.WriteLine(s2); StringBuilder s3 = new StringBuilder("Hello world"); s3.Remove(5, 6); Console.WriteLine(s3); } } }
结果: