码迷,mamicode.com
首页 > Windows程序 > 详细

C#字符串基础

时间:2020-05-10 11:05:39      阅读:66      评论:0      收藏:0      [点我收藏+]

标签:vs code   cti   tst   res   extend   字符   str   ini   UNC   


        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#字符串基础

标签:vs code   cti   tst   res   extend   字符   str   ini   UNC   

原文地址:https://www.cnblogs.com/ezhar/p/12862150.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!