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

C# 比較()

PHPz
PHPz原創
2024-09-03 15:18:14960瀏覽

在 C# 中,您可以使用 Compare() 方法來比較兩個字串。此整數值可以小於零、等於零或大於零。如果在給定的兩個字串中,第一個字串按排序順序位於第二個字串之前,則傳回值小於零,且傳回值等於零。如果兩個字串具有相同的值且 Compare() 方法的傳回值大於零;按排序順序,第二個字串位於第一個字串之後。

文法:

文法如下:

String.Compare(string1, string2);

其中 string1 是需要與第二個字串 string2 進行比較的第一個字串。

C# Compare() 的工作原理

  • 每當需要比較兩個字串的排序順序,並確定第一個字串按排序順序是否在第二個字串之前,或者第二個字串按排序順序是否在第一個字串後面,或兩個字串相等,我們使用C# 中的Compare() 方法。
  • 如果兩個字串的值相等,Compare() 方法將傳回零。
  • 如果在給定的兩個字串中,第一個字串按排序順序位於第二個字串之前,Compare() 方法將傳回一個小於零的值。

範例

下面給出的是提到的例子:

範例#1

C# 程式示範如何使用 Compare() 方法來比較兩個字串。

代碼:

using System;
//a class called check is defined
public class check
{
//main method is called within which three string variables are defined to store three different strings
public static void Main(string[] args)
{
string string1 = "Welcome";
string string2 = "to";
string string3 = "C#";
//compare() method is used to compare two strings at a given time which returns an integer value less than zero if the first string precedes the second string in the sorting order or returns an integer value equal to zero if the first string is equal to the second string or returns an integer value greater than zero if the first string is followed by the second string in the sorting order
Console.WriteLine("The result of comparing the string1 and string2 is: {0}",string.Compare(string1,string2));
Console.WriteLine("The result of comparing the string2 and string3 is: {0}",string.Compare(string2,string3));
Console.WriteLine("The result of comparing the string3 and string1 is: {0}",string.Compare(string3,string1));
}
}

輸出:

C# 比較()

說明:

  • Compare() 方法同時比較兩個字串。如果第一個字串按排序順序位於第二個字串之前,則此方法將傳回小於零的整數值。如果第一個字串與第二個字串相似,則它傳回一個等於零的整數值。最後,如果第一個字串按排序順序位於第二個字串之後,它會傳回一個大於零的整數值。

範例#2

C# 程式示範如何使用 Compare() 方法來比較兩個字串。

代碼:

using System;
//a class called check is defined
public class check
{
//main method is called within which three string variables are defined to store three different strings
public static void Main(string[] args)
{
string string1 = "Learning is fun";
string string2 = "Learning is fun";
string string3 = "fun";
//compare() method is used to compare two strings at a given time which returns an integer value less than zero if the first string precedes the second string in the sorting order or returns an integer value equal to zero if the first string is equal to the second string or returns an integer value greater than zero if the first string is followed by the second string in the sorting order
Console.WriteLine("The result of comparing the string1 and string2 is: {0}",string.Compare(string1,string2));
Console.WriteLine("The result of comparing the string2 and string3 is: {0}",string.Compare(string2,string3));
Console.WriteLine("The result of comparing the string3 and string1 is: {0}",string.Compare(string3,string1));
}
}

輸出:

C# 比較()

說明:

  • 在上面的程式中,定義了一個名為check的類別。然後呼叫main方法,其中定義了三個字串變數來儲存三個不同的字串。

優點

以下是優點:

  • 可以使用C#中的Compare()方法來決定字串在排序順序中的相對位置。
  • 如果給定的運算元是字串,並且我們想知道字串在排序順序中是在另一個字串之前還是在另一個字串之後,Compare() 方法是C# 中可用的最佳選項之一。

結論

在本教程中,我們透過程式設計範例及其輸出,透過定義、語法和Compare() 方法的工作原理了解了C# 中Compare() 方法的概念,以及在處理問題時在程式中使用Compare( ) 方法的優點帶字串。

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

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