• String类的使用 Part2


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

    结果:

  • 相关阅读:
    UVA-448
    算法提高-集合选取
    算法训练Maze
    UVA-10061
    树状数组
    前缀和
    【UVA
    统计Linux下的CPU状态信息
    Android_内部文件读取
    Android打开/data/目录以及导出文件
  • 原文地址:https://www.cnblogs.com/TangPro/p/3182760.html
Copyright © 2020-2023  润新知