1. 程式人生 > >深入C# String類

深入C# String類

深入C# String類

 

C#中的String類

他是專門處理字串的(String),他在System的名稱空間下,在C#中我們使用的是string

小寫的string只是大寫的String的一個別名(外號)使用大寫和小寫都是一樣的

 

 

常用的字串處理法

Java中常用的字串處理方法?

1)  IndexOf:字串的檢索

a)         IndexOf(String s):重頭開始檢索

b)         IndexOf(String s,startString s):從startString開始檢索

2)  獲取長度:.length()

3)  判斷.equals(引數)

4)  得到字串的子字元

subString(引數)

subString(引數1,引數2)

 

 

C#提供的字串比較的方法

1)  IndexOf:查詢某個字元在字串當中的位置

2)  subString:從字串中擷取子字元

3)  ToLower():轉換成小寫

4)  ToUpper():大寫

5)  Time():去空格

6)  Equals():比較字串值

If(name == “”)                    地址

If(name.Equals(String.Empty))值

 

“”和Empty的區別?

If(name == “”)                              分配一個長度為空的儲存空間

If(name.Equals(String.Empty)) 不會分配儲存空間

         判斷空字串的三種寫法?這三個的效能比較?

Name.Length ==0                      2

Name == String.Empty              1

Name == “”                                  3

7)  joi():連結字串

8)  split():分割

 

 

獲取郵箱使用者名稱

         需求:獲取郵箱的使用者名稱

                     兼用各種格式yes(YES)

                     迴圈執行

public void GetUserName()

{

    string code;//使用者選擇

    do{

        this.PickNameFoemEmail();

        Console.WriteLine("是否繼續?yes/no");

        code = Console.ReadLine();

        code = code.Trim().ToLower();

    }while(code.Equals("yes"));

}

public void PickNameFoemEmail()

{

    string emsil;// 獲取郵箱

    string name;//獲取使用者名稱

 

    Console.WriteLine("請輸入郵箱:");

    emsil = Console.ReadLine();

 

    Console.WriteLine("你的郵箱是{0}:",emsil);

 

 

    // 提取

    int posion = emsil.IndexOf("@");

    if (posion > 0)

    {

        name = emsil.Substring(0, posion);

 

        Console.WriteLine("你的郵箱地址是:{0}", name);

    }

    else

    {

        Console.WriteLine("你的郵箱格式錯誤");

    }

}

Class1 c = new Class1();

c.GetUserName();

 

Console.ReadKey();

                       

 

 

連線分割字串

Join     split

 

// 輸入的字串

string inputString;

// 分割後的字串陣列

string[] splitString;

// 連線後的

string joinString;

 

Console.WriteLine("請輸入字串,用空分開:");

 

inputString = Console.ReadLine();

splitString = inputString.Split(' ');

Console.WriteLine(@"\n分割後的:");

foreach (var item in splitString)

{

    Console.WriteLine(item);

}

joinString = string.Join("+連線+",splitString);

 

Console.WriteLine("\n連線後的字串:{0}",joinString);

 

 

 

@”\n轉義符”:忽略掉

 

 

 

Format格式化(不是清除的意思)

String name = “Tom”;

Console.WritrLine(“我的名字:{0},我的年齡{1}”,name,22);

{x}佔位符的方式去輸出

string name;

            string birthday;

            int height;

            string bloodType;

            string planet;

            string loveFood;

            string record;

 

            Console.WriteLine("歡迎來到“C#”的世界!");

            Console.WriteLine("請輸入你的個人資訊,我將為你建立個人檔案:");

            Console.Write("姓名:");

            name = Console.ReadLine();

            Console.Write("出生年月:(*年*月*日):");

            birthday = Console.ReadLine();

            Console.Write("身高(cm):");

            height = int.Parse(Console.ReadLine());

            Console.Write("星座:");

            planet = Console.ReadLine();

            Console.Write("血型:");

            bloodType = Console.ReadLine();

            Console.Write("喜歡的食物:");

            loveFood = Console.ReadLine();

 

            record = string.Format("姓名:{0}\n出生年月:{1}\n身高:{2}\n星座:{3}\n血型:{4}\n喜歡的食物:{5}",name,birthday,height,bloodType,planet,loveFood);

 

            Console.WriteLine("\n這是你的個人檔案:");

            Console.WriteLine(record);

 

            Console.ReadKey();

 

 

 

 

 

Grammar:String myString = string.Format(“格式化字串”,引數列表)

 

2 * 3 = 6

String myString = string.Format(“{0}乘以{1}等於{2}”,2,3 ,2 * 3);

 

 

如輸出貨幣

語法:

格式字串包括:固定文字和格式項

格式項

 

 

 

 

Console.WriteLine("{0}",50);

            Console.WriteLine(String.Format("{0,-8:F2}",50));

            Console.WriteLine(String.Format("{0,8:C2}", 50));

 

 

Format()方法的格式化字串中各種格式化定義符和示例

1)  C:貨幣格式

2)  D十進位制格式

3)  F小數點後固定位數

4)  用逗號隔開的數字

5)  百分比計數法

6)  十六進位制格式

Console.WriteLine("{0}",String.Format("{0:C3}",3000));

Console.WriteLine("{0}",String.Format("{0:D3}",2000));

Console.WriteLine("{0}", String.Format("{0:F3}", 2000));

Console.WriteLine("{0}", String.Format("{0:N}", 230000));

Console.WriteLine("{0}", String.Format("{0:P3}", 0.921867357621));

Console.WriteLine("{0}", String.Format("{0:X000}", 12));