C#字符串類String的使用(一)

概述

在c#中字符串做爲對象來處理,能夠經過String類來建立字符串對象。字符串必須包含在一對 " "(雙引號)以內。"1122" 、"nihao".c#

聲明字符串常量

注:聲明字符串變量必須通過初始化才能使用,不然編譯器會報出「使用了未賦值的變量」。code

string s;
鏈接多個字符串
//鏈接多個字符串
            String s1 = "hello";
            String s2 = "World";
            String s = s1 + "" + s2;
            Console.WriteLine(s);
            Console.ReadLine();
比較字符串
//字符串的比較
            //結果應爲false
            string str1 = "zhzfdx";
            string str2 = "zhzfdx@qq.com";
            Console.WriteLine((str1 == str2));
            Console.ReadLine();
//運用compare方法
            //相等爲0 小於-1 大於1
            //若長度相等比較比較字符串按照字典排序的規則,
            //在英文字典中前面的單詞小於後面的單詞
            string str1 = "zhzfdx";
            string str2 = "zhzfdx@qq.com";
            Console.WriteLine(String.Compare(str1, str1));
            Console.WriteLine(String.Compare(str1, str2));
            Console.WriteLine(String.Compare(str2, str1));
            Console.ReadLine();
//CompareTo方法:
            //以實例對象自己與指定字符串做比較

            Console.WriteLine(str1.CompareTo(str2));
            Console.ReadLine();
//Equals方法:主要用於比較兩個字符串是否相同
            //返回true 或者false
            Console.WriteLine(str1.Equals(str2));
            Console.WriteLine(String.Equals(str1, str1));
            Console.WriteLine(String.Equals(str1, str2));
            Console.ReadLine();

未完待續。。。。。。對象

相關文章
相關標籤/搜索