首頁  >  文章  >  後端開發  >  C# 中的修剪()

C# 中的修剪()

王林
王林原創
2024-09-03 15:15:17513瀏覽

在 C# 中,Trim() 方法用於刪除給定字串中的某些類型的資料。要從字串字元的開頭和結尾刪除字串中的空格,使用的語法是“public string Trim()”,要刪除給定字串中的特定字符,使用的語法是“public string Trim(char [ ] chararr)',其中'char[] chararr' 表示需要從特定字串中刪除的字元陣列。

字串修剪方法的語法

以下是我們如何在 C# 中使用 Trim() 刪除所有空格以及特定字元的語法。

1.刪除開頭和結尾的空格。

public string Trim()
2.刪除特定字元。

public string Trim(char[] chararr)
首先,它只用於刪除空格,這裡我們不需要提供任何參數。但如果要刪除特定字符,我們需要在數組中提供這些字符。 TRIM方法傳回一個新的字串,而不是修改目前實例的值,其中所有開始和結束的空白字元將被刪除。如果字串不包含任何字元且所有字元均由空格組成,則 Trim 方法傳回空。

Trim 在 C# 中如何運作?

以下範例展示如何在 C# 中實作 Trim()。

範例#1

在下面的範例中,呼叫 Trim 方法來刪除字串中的空格。在此範例中,無需提供任何參數,因為我們只想刪除任何字串的空格。 C# 中的字串是不可變的,因此使用該方法後原始字串將保持原樣。

代碼:

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();
}
}
}

輸出:

C# 中的修剪()

範例#2

在下面的範例中,Trim() 用於字串和字元類型。它刪除所有空格以及字元。例如,您可以看到使用該方法之前和之後的字串。定義了一組字符,指定我們要刪除的字符。它刪除 string 類型中的所有空格並修剪 char 類型中的所有字元。

代碼:

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();
}
}
}

輸出:

C# 中的修剪()

範例#3

以下範例展示如何在 C# 中實作 TrimStart()。 下面的範例中,TrimStart() 用於修剪字元和空格。在此方法中,僅刪除起始空格和字元。定義了一組字符,指定我們要刪除的字符。在輸出中,您可以看到它僅刪除了初始空格和字符,並保留了末尾出現的其他空格和字符。

代碼:

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();
}
}
}

輸出:

C# 中的修剪()

範例#4

以下範例展示如何在 C# 中實作 TrimEnd()。 下面的範例中,TrimEnd() 用於修剪空格和字元。 TrimStart() 用於修剪起始空格/字符,另一方面,TrimEnd() 用於修剪末尾出現的字符/空格。在輸出中,您可以看到它將保留原樣的空格,並刪除末尾出現的所有空格和字元。

代碼:

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();
}
}
}

輸出:

C# 中的修剪()

範例#5

下面的範例定義了一個字元陣列。當呼叫 Trim 方法時,數組中定義的元素已被刪除。輸出將是修剪數組中提到的所有字元後的字串。如果指定字串中存在定義的字符,則修剪將起作用,並在找到其他字符時終止。

代碼:

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# 中的修剪()

結論

所以Trim方法是一個去除空格和特定字元的字串方法。 TrimStart() 用於刪除字串開頭出現的空格和字符,而 TrimEnd() 用於刪除任何字串末尾出現的空格和字元。

以上是C# 中的修剪()的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:C# 中的列舉下一篇:C# 中的列舉