C#字符串基础


        static void Main(string[] args)
        {
            //How to run C# in VS Code?
            /*
            step 0: create a null folder and open it in vscode
            step 1: dotnet new console
            step 2: dotnet restore
            step 3: dotnet run
            */
            Console.WriteLine("Hello World!");

            ////////////////////////////////////

            //Common string is unchangable 
            string str = "hello//:www.world.edu.uk";
            int result = str.CompareTo("hello");//return 0 when 2 strings are the same
            result = string.Compare(str, "hello");//same as above
            Console.WriteLine(result);//0
            result = str.CompareTo("hallo");//return 1 when the first different letter-index in string is lager 
            result = string.Compare(str, "hallo");//same as above
            Console.WriteLine(result);//1
            result = str.CompareTo("hillo");//return -1 when the first different letter-index in string is smaller
            result = string.Compare(str, "hillo");//same as above
            Console.WriteLine(result);//-1
            string newStr = str.Replace("ll", "ff");//replacement won‘t change original string
            Console.WriteLine($"Original:{str}. New:{newStr}.");//original string is still the originale one
            string targetString = "world";
            int indexOfTargetString = str.IndexOf(targetString);//returns the index of the first letter in target string
            Console.WriteLine($"index of {targetString} is {indexOfTargetString}");//12
            indexOfTargetString = str.IndexOf(targetString + "X");//return -1 if no target string was found
            Console.WriteLine($"index of {targetString + "X"} is {indexOfTargetString}");//-1
            //Use them in comprohensive way such as below:
            if (!str.IndexOf(targetString).Equals(-1))
            {
                string m_targetString = str.Substring(str.IndexOf(targetString), targetString.Length);
                Console.WriteLine($"my target string from the sentence: {m_targetString}");
            }

            ////////////////////////////////////

            //Common string is unchangable and may cause wastes
            //We use string builder to create strings which often changes
            StringBuilder stringBuilder = new StringBuilder("Hello World!");
            StringBuilder stringBuilder2 = new StringBuilder(100);//open string space size of 100, space will be extended if exceeds
            StringBuilder stringBuilder3 = new StringBuilder("Hello World!", 100);//initialize and open 100 soze of string space, space will be extended if exceeds
            Console.WriteLine(stringBuilder);
            stringBuilder.Append("---By Alexander");//Effective
            Console.WriteLine(stringBuilder);
            stringBuilder.Insert(5, "*a lamp inserted*");
            Console.WriteLine(stringBuilder);
            stringBuilder.Replace("Hello", "Conia sayo");//This will affect the original string
            Console.WriteLine(stringBuilder);//original string has been changed
            stringBuilder.Clear();
            Console.WriteLine(stringBuilder);
        }

C#字符串基础

上一篇:C# 之 Ref Out


下一篇:Windows10 彻底删除 Jenkins - 简单可操作版