天天看點

C#程式設計:字元串相關處理

目錄導航

  • ​​建立字元串​​
  • ​​NULL與空字元串​​
  • ​​字元串搜尋​​
  • ​​字元串處理​​
  • ​​提取字元串​​
  • ​​從指定位置插入或删除字元​​
  • ​​特定字元或字元串替換​​
  • ​​分割字元串​​
  • ​​字元串比較​​

建立字元串

/// <summary>
        /// 建立字元串
        /// </summary>
        public static void CreateString() {

            string s1 = "hello";
            string s2 = "first line\r\nSecond Line";
            string s3 = @"\\ser\\file\\hello.cs";
            char[] ca = "hello world".ToCharArray();
            string s = new string(ca);
            
            System.Console.WriteLine(s1);
            System.Console.WriteLine(s2);
            System.Console.WriteLine(s3);
            System.Console.WriteLine(s);   
        }      

NULL與空字元串

/// <summary>
        /// Null和空字元串
        /// </summary>
        public static void NullString()
        {
            string emtry = "";
            System.Console.WriteLine(emtry == "");      //true
            System.Console.WriteLine(emtry == string.Empty);    //true
            System.Console.WriteLine(emtry.Length == 0);    //true

            string nullstring = null;
            System.Console.WriteLine(nullstring == null);   //true
            System.Console.WriteLine(nullstring == "");     //false

            try
            {
                try
                {
                    int i = nullstring.Length;
                    System.Console.WriteLine(i);   //抛出了這個NullReferenceException 異常
                }
                catch (System.NullReferenceException n1)
                {
                    throw new Exception("NullReferenceException {0}", n1);
                }
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
            }
            finally
            {

                System.Console.WriteLine("抛出了異常");
            }
        }      

字元串搜尋

/// <summary>
        /// 字元串搜尋
        /// </summary>
        public static void SortString()
        {
            System.Console.WriteLine("abcdef".IndexOf("cd"));   //不存在則為-1
        }      

字元串處理

提取字元串

從指定位置插入或删除字元

特定字元或字元串替換

分割字元串

字元串比較

/// <summary>
        /// 字元串處理
        /// </summary>
        public static void DealString()
        {
            //提取字元串
            string left3 = "123456".Substring(0, 3);    //left3="123"
            string mid3 = "12345".Substring(2);         //mid3="345"

            //從指定位置插入或删除字元
            string s1 = "helloworld".Insert(5, ", ");   //s1="hello, world"
            string s2 = s1.Remove(5, 2);            //s2="helloworld"

            //特定字元或字元串替換
            System.Console.WriteLine("to my home".Replace(" ", "?"));   //to?my?home

            //分割字元串
            string[] words = "The quick brown fox".Split();
            //Split預設情況下使用空白字元作為分隔符

            //Join的使用效果與Split相反
            string together = string.Join(",", words);
            System.Console.WriteLine(together); //The,quick,brown,fox

            //字元串比較
            string s3 = "hello world";
            System.Console.WriteLine(s3.CompareTo("hello world"));  //0
            System.Console.WriteLine(s3.CompareTo("Hello World"));  //-1
            System.Console.WriteLine(s3.CompareTo("123"));          //1 
        }