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);
        }
    }
}

结果:

String类的使用 Part2

 

方法:

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);
        }
    }
}

结果:

String类的使用 Part2

 

相关文章:

  • 2021-04-23
  • 2022-12-23
  • 2022-01-04
  • 2021-08-16
  • 2021-10-30
  • 2022-12-23
  • 2022-12-23
  • 2021-06-15
猜你喜欢
  • 2022-02-11
  • 2021-12-04
  • 2021-11-03
  • 2022-12-23
  • 2022-12-23
  • 2022-01-15
  • 2022-12-23
相关资源
相似解决方案