在 C# 中,Trim() 方法用於刪除給定字串中的某些類型的資料。要從字串字元的開頭和結尾刪除字串中的空格,使用的語法是“public string Trim()”,要刪除給定字串中的特定字符,使用的語法是“public string Trim(char [ ] chararr)',其中'char[] chararr' 表示需要從特定字串中刪除的字元陣列。
字串修剪方法的語法1.刪除開頭和結尾的空格。
public string Trim()2.刪除特定字元。
public string Trim(char[] chararr)首先,它只用於刪除空格,這裡我們不需要提供任何參數。但如果要刪除特定字符,我們需要在數組中提供這些字符。 TRIM方法傳回一個新的字串,而不是修改目前實例的值,其中所有開始和結束的空白字元將被刪除。如果字串不包含任何字元且所有字元均由空格組成,則 Trim 方法傳回空。
Trim 在 C# 中如何運作?
範例#1
代碼:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Trim { class Program { static void Main(string[] args) { string s1 = "Trim "; string s2 = " Trim "; string s3 = "Trim "; Console.WriteLine("Without using Trim:"); // before using Trim method Console.WriteLine(s1); Console.WriteLine(s2); Console.WriteLine(s3); Console.WriteLine(""); Console.WriteLine("After using Trim:"); // after using Trim Method Console.WriteLine(s1.Trim()); Console.WriteLine(s2.Trim()); Console.WriteLine(s3.Trim()); Console.ReadLine(); } } }
輸出:
範例#2
代碼:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Trim { class Program { static void Main(string[] args) { string str1 = " This"; // trim blank spaces string str2 = " is "; string str3 = " programming "; string str4 = " language "; Console.WriteLine("{0} {1} {2} {3}" ,str1, str2, str3, str4); Console.WriteLine("{0} {1} {2} {3}",str1.Trim(), str2.Trim(), str3.Trim(), str4.Trim()); char[] Chars = { '*', '#', ' ' }; // defining set of characters // Trim with Characters string str5 = "** Example #*#"; Console.WriteLine( str5); Console.WriteLine(str5.Trim(Chars)); Console.ReadLine(); } } }
輸出:
範例#3
代碼:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Trim { class Program { static void Main(string[] args) { // Trim Whitespaces string str1 = " This"; string str2 = " is "; string str3 = " C#"; string str4 = " programming "; Console.WriteLine("{0} {1} {2} {3}" ,str1, str2, str3, str4); Console.WriteLine("{0} {1} {2} {3}", str1.TrimStart(), str2.TrimStart(), str3.TrimStart(), str4.TrimStart()); char[] Chars = { '*', '#', ' ' }; // defining set of characters // Trim with Characters string str5 = "** Example #*#"; Console.WriteLine( str5); Console.WriteLine(str5.TrimStart(Chars)); Console.ReadLine(); } } }
輸出:
範例#4
代碼:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Trim { class Program { static void Main(string[] args) { string str1 = " This"; string str2 = " is "; string str3 = "C# "; string str4 = " programming "; Console.WriteLine("{0} {1} {2} {3}" ,str1, str2, str3, str4); Console.WriteLine("{0} {1} {2} {3}", str1.TrimEnd(), str2.TrimEnd(), str3.TrimEnd(), str4.TrimEnd()); char[] Chars = { '*', '#', ' ' }; // defining set of characters string str5 = "** Example #*#"; Console.WriteLine( str5); Console.WriteLine(str5.TrimEnd(Chars)); Console.WriteLine("\nPress Enter Key to Exit.."); Console.ReadLine(); } } }
輸出:
範例#5
代碼:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Trim { class Program { static void Main(string[] args) { char[] Trimarr = { '@', ',', '/', 'a', 'b', 'x', ' ' }; // defining set of characters string Trim = " ax trimdemo @ "; Console.WriteLine("Before:" + Trim); Console.WriteLine("After:" + Trim.Trim(Trimarr)); Console.ReadLine(); } } }
輸出:
結論
以上是C# 中的修剪()的詳細內容。更多資訊請關注PHP中文網其他相關文章!