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);//ReplaceStringBuilder 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);}} }
结果: