C# String方法及使用

在C#中,字串是以string類型的物件(Objects)形式存在,當宣告string時,事實上就是實例化(instantiate)一個字串物件。

可以透過多種方式宣告並初始化字串

{
  string str1;

  string temp = "I am very handsome";

  // 可放入null
  string str2 = null;

  // 放入檔案路徑
  string oldPath = "c:\\Program Files\\Pictures";

  // 如加上 @,則可使用單斜線
  string oldPath = @"c:\Program Files\Pictures";

  char[] letters = { 'A', 'B', 'C' };
  string order = new string(letters);

  // 如果要每輸出文字時,做換行,可加上 \r\n
  string rows = "Row 1\r\nRow 2\r\nRow 3";
  /* Output: 
       Row 1 
       Row 2 
       Row 3 
  */
}

如果是要兩個或多個以上的字串做合併時

{
    string s1 = "Hello";
    string s2 = " World!";
    string s3 = " My name is Jason.";
    string s4;
    int num = 1234;

    s4 = s1 + s2 + s3; // 輸出:Hello World! My name is Jason.

    s1 += s2 + s3; // 也可以用 += 簡化,輸出結果相同:Hello World! My name is Jason.

    s1 += s2 + s3 + num; // 如果加上數值時,輸出:Hello World! My name is Jason.1234

}

在C#中,string也提供許多方法

Format

{
   double temp = 25.6;
   string temperature = String.Format("The temperature is {0}°C.", temp);
   Console.WriteLine(temperature);
   // 輸出: The temperature is 25.6°C.

   string info = String.Format("At {0}, the temperature is {1}°C.",DateTime.Now, 25.8);
   Console.WriteLine(info);
   // 輸出: At 15/12/2018 10:23:05 AM, the temperature is 25.8°C.

}

Length

取得字串長度

{
   string temp = "Hi! My name is Jason.";
   int lenght = temp.Length;

   Console.WriteLine(lenght); // 21
}

IndexOf

搜尋該關鍵字所在起始位置的索引值

{
    string temp = "Hi! My name is Jason.";
    int index = temp.IndexOf("m");
    Console.WriteLine(index); // 9
}

除了上述方式之外,也可以透過跟陣列一樣方式,字串的索引起始值也是0,可以用操作索引來取得字元

{
   string temp = "Hi! My name is Jason.";
   Console.WriteLine(temp[3]); // 輸出: !
}

Insert

將關鍵字插入指定索引位置

{
   string temp = "Hi! My name is Jason.";
   string str = temp.Insert(0, "Happy New Year ");
   Console.WriteLine(str); // Happy New Year Hi! My name is Jason.
}

Remove

清除索引位置之後的字串

{
   string temp = "Hi! My name is Jason.";
   string newStr = temp.Remove(3);
   Console.WriteLine(newStr); // Hi!
}

Replace

將原字串取代為新字串

{
   string temp = "Hi! My name is Jason.";
   string str = temp.Replace("Hi!", "Hello!");
   Console.WriteLine(str); // Hello! My name is Jason.
}

Substring

從指定索引位置取得指定長度的字串

{
   string temp = "Hi! My name is Jason.";
   string Findtemp = temp.Substring(4,7); // 索引起始位置4,往後取得7個字串值
   Console.WriteLine(Findtemp); // My name
}

Contains

判斷是否包含該關鍵字

{
   string temp = "Hi! My name is Jason.";
   if (temp.Contains("Jason"))
   {
     Console.WriteLine("OK");
   }
   else
   {
     Console.WriteLine("NO");
   }
   
   // OK
}

Equals

判斷兩個String物件是否具有相同的值

{
   string str = "Hi";
   if (str.Equals("Hi"))
   {
      Console.WriteLine("相同");
   }


   // 如果判斷字串,並加上判斷大小寫時
   string account = "Hello";
   string UserInput = "hello";

   bool IsEquals = account.Equals(UserInput, StringComparison.CurrentCultureIgnoreCase);
    // StringComparison.CurrentCultureIgnoreCase => 加入此行判斷時就能不限定大小寫,移除則會判斷大小寫

   Console.WriteLine(IsEquals);

}

Compare

比較兩個指定的String對象,並返回一個整數

{
   string str = "Hello";

   // 比較兩個值,相等則傳回0
   if (string.Compare(str, "Hello") == 0)
   {
      Console.WriteLine("相同");
   }

   // 也可寫成
   string str = "Hello";
   string compareName = "Hello";

   if(str.CompareTo(compareName) == 0)
   Console.WriteLine("相等");

   // 如果不區分大小寫
   string str = "Hello";
   string compareName = "hello";

   if (string.Compare(str, compareName, true) == 0)
   {
      Console.WriteLine("相同");
   }

   
}

 

 

新人發文,文章敘述如有錯誤及觀念不正確,請不吝嗇指教,感謝~

參考文件 https://docs.microsoft.com/en-us/dotnet/api/system.string?view=netcore-2.2