首頁  >  文章  >  後端開發  >  C# StartsWith()

C# StartsWith()

PHPz
PHPz原創
2024-09-03 15:15:58342瀏覽

用於檢查給定字串的開頭實例是否與另一個字串匹配的方法在 C# 中稱為 StartsWith() 方法。如果字串與給定字串的起始實例匹配,則傳回 true;使用 StartsWith() 方法傳回 false。如果給定字串的起始實例與其他字串不匹配,則可以透過使用 C# 中的 for every 循環一次檢查多個字串,並且該方法也可以使用不同數量的字串進行重載。不同資料類型的參數作為參數傳遞給方法。

文法:

C# StartsWith() 方法的語法如下:

public bool StartsWith(String string_name);

其中 string_name 是與給定字串的開頭實例相符的字串的名稱。

C# StartsWith() 的工作原理

C# StartsWith() 方法的工作原理如下:

  • 每當需要確定任何字串的開頭是否與給定字串的開頭相符時,我們都會使用 C# 中的 StartsWith() 方法。
  • C# 中的 StartsWith() 方法是一個字串方法,傳回一個布林值,該值要麼為 true,要麼為 false。
  • 如果字串與給定字串的開頭實例匹配,則 StartsWith() 方法傳回 true。
  • 如果字串與給定字串的開頭實例匹配,StartsWith() 方法將傳回 false。

C# StartsWith() 範例

以下是範例:

範例#1

C# 程式示範如何使用 StartsWith() 方法來檢查任何字串的開頭是否與給定字串的開頭相符:

代碼:

using System;
//a class called check is defined
public class check
{
//main method is called within which a string variable is defined to store the string value which is checked to see if there is a match of beginning instance in this string with reference to the other string compared
public static void Main(string[] args)
{
string string1 = "Welcome to C#";
//StartsWith() method is used to check if there is a match to the beginning instance of the given string with reference to the other string passed as a parameter to it
bool bval1 = string1.StartsWith("Welcome");
bool bval2 = string1.StartsWith("w");
Console.WriteLine("The string Welcome matches the beginning instance of the given string Welcome to C#: {0}", bval1);
Console.WriteLine("The string w matches the beginning instance of the given string Welcome to C#: {0}", bval2);
}
}

輸出:

C# StartsWith()

在上面的程式中,定義了一個名為check的類別。然後呼叫 main 方法,在該方法中定義一個字串變數來儲存字串值,檢查該字串值以查看該字串中的起始實例是否與所比較的另一個字串相符。然後 StartsWith() 方法用於檢查給定字串的起始實例是否與作為參數傳遞給它的另一個字串相符。將第一個字串Welcome 與給定字串Welcome to C# 進行檢查,以查找Welcome to C# 中是否存在與字串Welcome 匹配的起始實例,並且傳回的輸出為True,因為Welcome 出現在Welcome to C#中,而當對照w 檢查時給定字串Welcome to C#,查找Welcome to C#中是否存在與字串w匹配的起始實例,並且傳回的輸出為False,因為Welcome to C#中不存在w。

範例#2

範例 2:C# 程式示範如何使用 StartsWith() 方法來檢查任何字串的開頭是否與給定字串的開頭相符:

代碼:

using System;
//a class called check is defined
public class check
{
//main method is called within which a string variable is defined to store the string value which is checked to see if there is a match of beginning instance in this string with reference to the other string compared
public static void Main(string[] args)
{
string string1 = "Learning is fun";
//StartsWith() method is used to check if there is a match to the beginning instance of the given string with reference to the other string passed as a parameter to it
bool bval1 = string1.StartsWith("l");
bool bval2 = string1.StartsWith("Learning");
Console.WriteLine("The string l matches the beginning instance of the given string Welcome to C#: {0}", bval1);
Console.WriteLine("The string Learning matches the beginning instance of the given string Welcome to C#: {0}", bval2);
}
}

輸出:

C# StartsWith()

在上面的程式中,定義了一個名為check的類別。然後呼叫 main 方法,在該方法中定義一個字串變數來儲存字串值,檢查該字串值以查看該字串中的起始實例是否與所比較的另一個字串相符。然後 StartsWith() 方法用於檢查給定字串的起始實例是否與作為參數傳遞給它的另一個字串相符。第一個字串l 根據給定字串進行檢查Learning if fun 來尋找Learning is fun 中是否存在與字串l 相符的起始實例,並且傳回的輸出為False,因為l 不存在於Learning is fun 中,而當Learning is fun 進行檢查時給定的字串Learning is fun 尋找Learning is fun 中是否存在與字串Learning 匹配的開始實例,並且傳回的輸出為True,因為Learning 存在於Learning is fun 中。

在 C# 中使用 StartsWith() 方法有幾個優點。他們是:

  • StartsWith() 方法用於檢查給定字串的開頭實例以及對另一個字串的引用,同時考慮字母的大小寫。
  • StartsWith() 方法可用於透過使用 for 每個迴圈來檢查給定字串的開始實例,同時引用多個字串。

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

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