C# 字串 – 在本文中,我將向你展示在c#程式設計中, 字串普遍都會使用的11方法, 當中的功能為在字串上進行插入, 移除, 取代, 移除空白, 補上空白, 轉換成大小寫, 合併分割
方法 | 例子: |
插入字串: String.Insert(Int32, String) String.Insert(“從第幾個字開始加入”, “加入的字串”): | string str = “I can fly”; Console.WriteLine(str.Insert(2,”believe I “)); //結果: I believe I can fly |
移除文字: String.Remove(Int32,Int32) String.Remove(“從第幾個開始移除”, 移除多少字元): | string str = “I believe I can fly”; Console.WriteLine(str.Remove(2, 10)); //結果: I can fly |
取代字串: String.Replace(String) String.Replace(取代的字串目標,新的字串) | string str = “I am sure I can fly”; string strResult = str.Replace(“am sure”, “believe”); Console.WriteLine(strResult); //結果: I believe I can fly |
取代字串 (忽略大小寫): Regex.Replace(String, String , String, RegexOptions.IgnoreCase); Regex.Replace(輸入字串, 取代的字串目標, 新的字串, RegexOptions.IgnoreCase); | string str = “I am sure I can fly”; string strResult = Regex.Replace(str, “AM SURE”, “believe”, RegexOptions.IgnoreCase); Console.WriteLine(strResult); //結果: I believe I can fly |
移除字串兩邊的空白: String.Trim() | string str = ” $10000 “; string strResult = str.Trim(); Console.WriteLine(strResult); //結果: “$10000” |
移除字串開頭的空白: String.TrimStart () | string str = ” $10000 “; string strResult = str.TrimStart(); Console.WriteLine(strResult); //結果: “$10000 ” |
移除字串結尾的空白: String.TrimEnd () | string str = ” $10000 “; string strResult = str.TrimEnd(); Console.WriteLine(strResult); //結果: ” $10000″ |
為字串左邊補上空白: String.PadLeft(Int32) String.PadLeft(字串總數包括補上的空白格) | string str = “$10000”; string strResult = str.PadLeft(10); Console.WriteLine(strResult); //結果: ” $10000″ |
為字串右邊補上空白: String.PadRight(Int32) String.PadRight(字串總數包括補上的空白格) | string str = “$10000”; string strResult = str. PadRight(10); Console.WriteLine(strResult); //結果: “$10000 ” |
為字串左邊補上字元: String.PadLeft(Int32, char) String.PadLeft(字串總數包括補上的空白格) | string str = “$10000”; string strResult = str.PadLeft(10, ‘-‘); Console.WriteLine(strResult); //結果: “—-$10000” |
為字串右邊補上字元: String.PadRight(Int32, char) String.PadRight(字串總數包括補上的空白格) | string str = “$10000”; string strResult = str. PadRight(10, ‘-‘); Console.WriteLine(strResult); //結果: “$10000—-” |
將字串轉換成小寫: String.ToLower() | string str = “I believe I can fly”; string strResult = str.ToLower(); Console.WriteLine(strResult); //結果: i believe i can fly |
將字串轉換成大寫: String.ToUpper() | string str = “I believe I can fly”; string strResult = str.ToUpper(); Console.WriteLine(strResult); //結果: I BELIEVE I CAN FLY |
將字串維度合併: String.Join(String, String[]) String.Split(中間字串, 合併字串維度) | string[] strs = new string[]{“I believe”, “I can fly”}; string strResult = String.Join(” “, strs); Console.WriteLine(strResult); //結果: I BELIEVE I CAN FLY |
將字串分割: String.Split(char) String.Split(‘用來分割的字元’) | string str = “I believe !I can fly”; string[] strResult = str.Split(‘!’); foreach (var item in strResult) Console.WriteLine(item); //結果: //I believe //I Can fly |
將字串分割: Regex.Split(String, String) Regex.Split(分割字串對象,”用來分割的字串”) | string str = “I believe that I can fly”; string[] strResult = Regex.Split(str, ” that “); foreach (var item in strResult) Console.WriteLine(item); //結果: //I believe //I Can fly |
其他相關:
C# 字串String功能速查表 – 尋找, 擷取, 對比, 組合
參考資料: https://docs.microsoft.com/en-us/dotnet/api/system.string?view=net-6.0