如何在 C# 中比较字符串?
字符串是字符的集合,用于存储纯文本。与 C 或 C++ 不同,C# 中的字符串不以空字符结尾。字符串对象的最大大小取决于系统的内部架构。后面跟“字符串”声明的变量实际上是字符串类的对象。
如何实例化一个字符串对象?
我们可以通过使用变量名后跟“ 字符串 ”关键字来创建字符串类的对象。
句法:
string myString;
我们也可以在声明的时候初始化一个字符串对象。
句法:
string myString = “GeeksForGeeks”;
本文重点介绍如何在 C# 中比较字符串。例如,我们得到三个字符串“GeeksforGeeks”、“Geeks”和“GeeksforGeeks”。显然第一个和最后一个字符串是相同的。在 C# 中比较字符串的方法有很多种,下面将详细介绍其中五种方法。
方法一:使用 String.Equals() 方法
String 类在 .NET 基类库中指定。换句话说, String 对象是 System.Char 对象的顺序集合,它们表示字符串。 System.String 类是不可变的,即一旦创建了它的状态,我们就无法对其进行更改。 String.Equals() 方法是 String 类的方法。此方法将要比较的两个字符串作为参数。它返回一个逻辑值,真或假,借助它我们可以确定给定的字符串是否相同。
句法:
String.Equals(myString1, myString2)
参数:它有两个参数 myString1:第一个字符串和 myString2:第二个字符串
返回类型:该方法的返回类型为布尔值。如果两个字符串相等则为真,如果两个字符串不同则为假
例子:
C#
// C# program to illustrate the working of
// String.Equals() method to compare two strings
using System;
class GFG{
static public void Main()
{
// Initialize a string
string myString1 = "GeeksforGeeks";
// Initialize another string
string myString2 = "Geeks";
// Initialize a string
string myString3 = "GeeksforGeeks";
// If this method returns true
// Print both string are same
if (String.Equals(myString1, myString2))
Console.WriteLine($"{myString1} and {myString2} are same.");
// If this method returns false
// Print both string are different
else
Console.WriteLine($"{myString1} and {myString2} are different.");
// If this method returns true
// Print both string are same
if (String.Equals(myString1, myString3))
Console.WriteLine($"{myString1} and {myString3} are same.");
// If this method returns false
// Print both string are different
else
Console.WriteLine($"{myString1} and {myString3} are different.");
// If this method returns true
// Print both string are same
if (String.Equals(myString2, myString3))
Console.WriteLine($"{myString2} and {myString3} are same.");
// If this method returns false
// Print both string are different
else
Console.WriteLine($"{myString2} and {myString3} are different.");
}
}
C#
// C# program to illustrate the working of
// String.Compare() method to compare two strings
using System;
class GFG{
static public void Main()
{
// Initialize a string
string myString1 = "GeeksforGeeks";
// Initialize another string
string myString2 = "Geeks";
// Initialize another string
string myString3 = "GeeksforGeeks";
// If value returned by this method is equal to 0
// Print both string are same
if (String.Compare(myString1, myString2) == 0)
Console.WriteLine($"{myString1} and {myString2} are same.");
// If value returned by this method is less than 0
// Then print first string is smaller than the second string
else if(String.Compare(myString1, myString2) < 0)
Console.WriteLine(
$"{myString1} is lexicographically smaller than {myString2}.");
// If value returned by the method is greater than 0
// Then print first string is greater than the second string
else
Console.WriteLine(
$"{myString1} is lexicographically greater than {myString2}.");
// If value returned by this method is equal to 0
// Print both string are same
if (String.Compare(myString1, myString3) == 0)
Console.WriteLine($"{myString1} and {myString3} are same.");
// If value returned by this method is less than 0
// Then print first string is smaller than the second string
else if (String.Compare(myString1, myString3) < 0)
Console.WriteLine(
$"{myString1} is lexicographically smaller than {myString3}.");
// If value returned by the method is greater than 0
// Then print first string is greater than the second string
else
Console.WriteLine(
$"{myString1} is lexicographically greater than {myString3}.");
// If value returned by this method is equal to 0
// Print both string are same
if (String.Compare(myString2, myString3) == 0)
Console.WriteLine($"{myString2} and {myString3} are same.");
// If value returned by this method is less than 0
// Then print first string is smaller than the second string
else if (String.Compare(myString2, myString3) < 0)
Console.WriteLine(
$"{myString2} is lexicographically smaller than {myString3}.");
// If value returned by the method is greater than 0
// Then print first string is greater than the second string
else
Console.WriteLine(
$"{myString2} is lexicographically greater than {myString3}.");
}
}
C#
// C# program to illustrate the working of
// CompareTo() method to compare two strings
using System;
class GFG{
static public void Main()
{
// Initialize a string
string myString1 = "GeeksforGeeks";
// Initialize another string
string myString2 = "Geeks";
// Initialize another string
string myString3 = "GeeksforGeeks";
// If value returned by this method is equal to 0
// Then display both strings are equal
if (myString1.CompareTo(myString2) == 0)
Console.WriteLine($"{myString1} and {myString2} are same.");
// If value returned by this method is less than 0
// Then print the first string is smaller than the second string
else if (myString1.CompareTo(myString2) < 0)
Console.WriteLine(
$"{myString1} is lexicographically smaller than {myString2}.");
// If value returned by this method is greater than 0
// Then print the first string is greater than the second string
else
Console.WriteLine(
$"{myString1} is lexicographically greater than {myString2}.");
// If value returned by this method is equal to 0
// Then print both strings are equal
if (myString1.CompareTo(myString2) == 0)
Console.WriteLine($"{myString1} and {myString3} are same.");
// If value returned by this method is less than 0
// Then print the first string is smaller than the second string
else if(myString1.CompareTo(myString2) < 0)
Console.WriteLine(
$"{myString1} is lexicographically smaller than {myString3}.");
// If value returned by this method is greater than 0
// Then print the first string is greater than the second string
else
Console.WriteLine(
$"{myString1} is lexicographically greater than {myString3}.");
// If value returned by this method is equal to 0
// Then display both strings are equal
if (myString1.CompareTo(myString2) == 0)
Console.WriteLine($"{myString1} and {myString2} are same.");
// If value returned by this method is less than 0
// Then print the first string is smaller than the second string
else if (myString1.CompareTo(myString2) < 0)
Console.WriteLine(
$"{myString2} is lexicographically smaller than {myString3}.");
// If value returned by this method is greater than 0
// Then print the first string is greater than the second string
else
Console.WriteLine(
$"{myString2} is lexicographically greater than {myString3}.");
}
}
C#
// C# program to illustrate the working of Compare() method
// of StringComparer class to compare two strings
using System;
class GFG{
static public void Main()
{
// Declare an object of class StringComparer
StringComparer myComparer = StringComparer.OrdinalIgnoreCase;
// Initialize a string
string myString1 = "GeeksforGeeks";
// Initialize another string
string myString2 = "Geeks";
// Initialize another string
string myString3 = "GeeksforGeeks";
// Compare strings using Compare method
// on the instantiated object
// If this method returns 0
// Print both strings are same
if (myComparer.Compare(myString1, myString2) == 0)
Console.WriteLine($"{myString1} and {myString2} are same.");
// If value returned by this method is smaller than 0
// Then print the first string is smaller than the second string
else if (myComparer.Compare(myString1, myString2) < 0)
Console.WriteLine(
$"{myString1} is lexicographically smaller than {myString2}.");
// If value returned by this method is smaller than 0
// Then print the first string is smaller than the second string
else
Console.WriteLine(
$"{myString1} is lexicographically greater than {myString2}.");
// If this method returns 0
// Print both strings are same
if (myComparer.Compare(myString1, myString3) == 0)
Console.WriteLine($"{myString1} and {myString3} are same.");
// If value returned by this method is smaller than 0
// Then print the first string is smaller than the second string
else if (myComparer.Compare(myString1, myString3) < 0)
Console.WriteLine(
$"{myString1} is lexicographically smaller than {myString3}.");
// If value returned by this method is smaller than 0
// Then print the first string is smaller than the second string
else
Console.WriteLine(
$"{myString1} is lexicographically greater than {myString3}.");
// If this method returns 0
// Print both strings are same
if (myComparer.Compare(myString2, myString3) == 0)
Console.WriteLine($"{myString2} and {myString3} are same.");
// If value returned by this method is smaller than 0
// Then print the first string is smaller than the second string
else if (myComparer.Compare(myString2, myString3) < 0)
Console.WriteLine(
$"{myString2} is lexicographically smaller than {myString3}.");
// If value returned by this method is greater than 0
// Then print the first string is greater than the second string
else
Console.WriteLine(
$"{myString2} is lexicographically greater than {myString3}.");
}
}
C#
// C# program to illustrate the working of
// custom Compare() method to compare two strings
using System;
class GFG{
// Compare method to compare two strings
static public int Compare(string myString1, string myString2)
{
// len stores minimum of two string lengths
int len = Math.Min(myString1.Length, myString2.Length);
// Iterate over all characters
for(int index = 0; index < len; index++)
{
// If the first not matched character of first
// string is smaller than the second string then
// return -1
if (myString1[index] < myString2[index])
return -1;
// If the first not matched character of first
// string is greater than the second string then
// return 1
else if (myString1[index] > myString2[index])
return 1;
}
// If lengths are equal
// Return 0
if (myString1.Length == myString2.Length)
return 0;
// If length of first string is smaller than the second string
// then return -1
// If length of first string is greater than the second string
// then return 1
return ((myString1.Length < myString2.Length) ? -1 : 1);
}
// Driver code
static public void Main()
{
// Initialize a string
string myString1 = "GeeksforGeeks";
// Initialize another string
string myString2 = "Geeks";
// Initialize another string
string myString3 = "GeeksforGeeks";
// If value returned by this method is equal to 0
// Then print both strings are same
if (Compare(myString1, myString2) == 0)
Console.WriteLine($"{myString1} and {myString2} are same.");
// If value returned by this method is smaller than 0
// Then print the first string is smaller than the second string
else if (Compare(myString1, myString3) < 0)
Console.WriteLine(
$"{myString1} is lexicographically smaller than {myString2}.");
// If value returned by this method is greater than 0
// Then print the first string is greater than the second string
else
Console.WriteLine(
$"{myString1} is lexicographically greater than {myString2}.");
// If value returned by this method is equal to 0
// Then print both strings are same
if (Compare(myString1, myString3) == 0)
Console.WriteLine($"{myString1} and {myString3} are same.");
// If value returned by this method is smaller than 0
// Then print the first string is smaller than the second string
else if (Compare(myString1, myString3) < 0)
Console.WriteLine(
$"{myString1} is lexicographically smaller than {myString3}.");
// If value returned by this method is greater than 0
// Then print the first string is greater than the second string
else
Console.WriteLine(
$"{myString1} is lexicographically greater than {myString3}.");
// If value returned by this method is equal to 0
// Then print both strings are same
if (Compare(myString2, myString3) == 0)
Console.WriteLine($"{myString2} and {myString3} are same.");
// If value returned by this method is smaller than 0
// Then print the first string is smaller than the second string
else if (Compare(myString2, myString3) < 0)
Console.WriteLine(
$"{myString2} is lexicographically smaller than {myString3}.");
// If value returned by this method is greater than 0
// Then print the first string is greater than the second string
else
Console.WriteLine(
$"{myString2} is lexicographically greater than {myString3}.");
}
}
GeeksforGeeks and Geeks are different.
GeeksforGeeks and GeeksforGeeks are same.
Geeks and GeeksforGeeks are different.
方法 2:使用 String.Compare() 方法
该方法也在 String 类下定义。此方法还将两个要比较的字符串作为参数。它根据传递给方法的字符串返回一个数值。此方法提供有关比较字符串的详细信息,这就是它优于String.equals()方法的原因。
句法:
String.Compare(myString1, myString2)
参数:它有两个参数 myString1:第一个字符串和 myString2:第二个字符串
返回类型:此方法的返回类型为整数。这将是:
- 小于零:如果第一个字符串按字典顺序小于第二个字符串。
- 零:如果两个字符串相等。
- 大于零:如果第一个字符串按字典顺序大于第二个字符串。
例子:
C#
// C# program to illustrate the working of
// String.Compare() method to compare two strings
using System;
class GFG{
static public void Main()
{
// Initialize a string
string myString1 = "GeeksforGeeks";
// Initialize another string
string myString2 = "Geeks";
// Initialize another string
string myString3 = "GeeksforGeeks";
// If value returned by this method is equal to 0
// Print both string are same
if (String.Compare(myString1, myString2) == 0)
Console.WriteLine($"{myString1} and {myString2} are same.");
// If value returned by this method is less than 0
// Then print first string is smaller than the second string
else if(String.Compare(myString1, myString2) < 0)
Console.WriteLine(
$"{myString1} is lexicographically smaller than {myString2}.");
// If value returned by the method is greater than 0
// Then print first string is greater than the second string
else
Console.WriteLine(
$"{myString1} is lexicographically greater than {myString2}.");
// If value returned by this method is equal to 0
// Print both string are same
if (String.Compare(myString1, myString3) == 0)
Console.WriteLine($"{myString1} and {myString3} are same.");
// If value returned by this method is less than 0
// Then print first string is smaller than the second string
else if (String.Compare(myString1, myString3) < 0)
Console.WriteLine(
$"{myString1} is lexicographically smaller than {myString3}.");
// If value returned by the method is greater than 0
// Then print first string is greater than the second string
else
Console.WriteLine(
$"{myString1} is lexicographically greater than {myString3}.");
// If value returned by this method is equal to 0
// Print both string are same
if (String.Compare(myString2, myString3) == 0)
Console.WriteLine($"{myString2} and {myString3} are same.");
// If value returned by this method is less than 0
// Then print first string is smaller than the second string
else if (String.Compare(myString2, myString3) < 0)
Console.WriteLine(
$"{myString2} is lexicographically smaller than {myString3}.");
// If value returned by the method is greater than 0
// Then print first string is greater than the second string
else
Console.WriteLine(
$"{myString2} is lexicographically greater than {myString3}.");
}
}
GeeksforGeeks is lexicographically greater than Geeks.
GeeksforGeeks and GeeksforGeeks are same.
Geeks is lexicographically smaller than GeeksforGeeks.
方法 3:使用 CompareTo() 方法
这是在 String 类下定义的实例方法,它直接应用于字符串或字符串对象。它根据要比较的字符串返回一个数值。此方法提供有关比较字符串的详细信息,这就是它优于String.equals()方法的原因。
句法:
myString1.CompareTo(myString2)
参数:它采用一个参数,即 myString2:第二个字符串
返回类型:此方法的返回类型为整数。这将是:
- 小于零:如果第一个字符串按字典顺序小于第二个字符串。
- 零:如果两个字符串相等。
- 大于零:如果第一个字符串按字典顺序大于第二个字符串。
例子:
C#
// C# program to illustrate the working of
// CompareTo() method to compare two strings
using System;
class GFG{
static public void Main()
{
// Initialize a string
string myString1 = "GeeksforGeeks";
// Initialize another string
string myString2 = "Geeks";
// Initialize another string
string myString3 = "GeeksforGeeks";
// If value returned by this method is equal to 0
// Then display both strings are equal
if (myString1.CompareTo(myString2) == 0)
Console.WriteLine($"{myString1} and {myString2} are same.");
// If value returned by this method is less than 0
// Then print the first string is smaller than the second string
else if (myString1.CompareTo(myString2) < 0)
Console.WriteLine(
$"{myString1} is lexicographically smaller than {myString2}.");
// If value returned by this method is greater than 0
// Then print the first string is greater than the second string
else
Console.WriteLine(
$"{myString1} is lexicographically greater than {myString2}.");
// If value returned by this method is equal to 0
// Then print both strings are equal
if (myString1.CompareTo(myString2) == 0)
Console.WriteLine($"{myString1} and {myString3} are same.");
// If value returned by this method is less than 0
// Then print the first string is smaller than the second string
else if(myString1.CompareTo(myString2) < 0)
Console.WriteLine(
$"{myString1} is lexicographically smaller than {myString3}.");
// If value returned by this method is greater than 0
// Then print the first string is greater than the second string
else
Console.WriteLine(
$"{myString1} is lexicographically greater than {myString3}.");
// If value returned by this method is equal to 0
// Then display both strings are equal
if (myString1.CompareTo(myString2) == 0)
Console.WriteLine($"{myString1} and {myString2} are same.");
// If value returned by this method is less than 0
// Then print the first string is smaller than the second string
else if (myString1.CompareTo(myString2) < 0)
Console.WriteLine(
$"{myString2} is lexicographically smaller than {myString3}.");
// If value returned by this method is greater than 0
// Then print the first string is greater than the second string
else
Console.WriteLine(
$"{myString2} is lexicographically greater than {myString3}.");
}
}
GeeksforGeeks is lexicographically greater than Geeks.
GeeksforGeeks is lexicographically greater than GeeksforGeeks.
Geeks is lexicographically greater than GeeksforGeeks.
方法四:使用 StringComparer 类的 compare() 方法
此方法在 StringComparer 类下定义。我们可以创建这个类的一个对象,借助它我们可以使用 Compare() 方法来比较两个字符串。此方法提供有关比较字符串的详细信息,这就是它优于String.equals()方法的原因。
句法:
StringComparer myComparer = StringComparer.OrdinalIgnoreCase;
myComparer.Compare(myString1, myString2);
参数:它有两个参数 myString1:第一个字符串和 myString2:第二个字符串
返回类型:此方法的返回类型为整数。这将是:
- 小于零:如果第一个字符串按字典顺序小于第二个字符串。
- 零:如果两个字符串相等。
- 大于零:如果第一个字符串按字典顺序大于第二个字符串。
例子:
C#
// C# program to illustrate the working of Compare() method
// of StringComparer class to compare two strings
using System;
class GFG{
static public void Main()
{
// Declare an object of class StringComparer
StringComparer myComparer = StringComparer.OrdinalIgnoreCase;
// Initialize a string
string myString1 = "GeeksforGeeks";
// Initialize another string
string myString2 = "Geeks";
// Initialize another string
string myString3 = "GeeksforGeeks";
// Compare strings using Compare method
// on the instantiated object
// If this method returns 0
// Print both strings are same
if (myComparer.Compare(myString1, myString2) == 0)
Console.WriteLine($"{myString1} and {myString2} are same.");
// If value returned by this method is smaller than 0
// Then print the first string is smaller than the second string
else if (myComparer.Compare(myString1, myString2) < 0)
Console.WriteLine(
$"{myString1} is lexicographically smaller than {myString2}.");
// If value returned by this method is smaller than 0
// Then print the first string is smaller than the second string
else
Console.WriteLine(
$"{myString1} is lexicographically greater than {myString2}.");
// If this method returns 0
// Print both strings are same
if (myComparer.Compare(myString1, myString3) == 0)
Console.WriteLine($"{myString1} and {myString3} are same.");
// If value returned by this method is smaller than 0
// Then print the first string is smaller than the second string
else if (myComparer.Compare(myString1, myString3) < 0)
Console.WriteLine(
$"{myString1} is lexicographically smaller than {myString3}.");
// If value returned by this method is smaller than 0
// Then print the first string is smaller than the second string
else
Console.WriteLine(
$"{myString1} is lexicographically greater than {myString3}.");
// If this method returns 0
// Print both strings are same
if (myComparer.Compare(myString2, myString3) == 0)
Console.WriteLine($"{myString2} and {myString3} are same.");
// If value returned by this method is smaller than 0
// Then print the first string is smaller than the second string
else if (myComparer.Compare(myString2, myString3) < 0)
Console.WriteLine(
$"{myString2} is lexicographically smaller than {myString3}.");
// If value returned by this method is greater than 0
// Then print the first string is greater than the second string
else
Console.WriteLine(
$"{myString2} is lexicographically greater than {myString3}.");
}
}
GeeksforGeeks is lexicographically greater than Geeks.
GeeksforGeeks and GeeksforGeeks are same.
Geeks is lexicographically smaller than GeeksforGeeks.
方法5:逐个字符字符(使用自定义比较方法)
字符串可以逐个字符进行字符。按照以下步骤使用自定义比较方法比较两个字符串。
- 在 main 方法之外声明一个静态方法Compare 。将此方法的返回类型设置为 int。
- 将变量len初始化为两个字符串长度中的最小值。
- 使用 for 循环遍历index = 0到index = len – 1 。在每次迭代中比较字符串的对应字符。
- 如果索引处第一个字符串的第一个不匹配字符小于索引处第二个字符串的字符,那么我们将返回 -1。
- 如果第一个字符串在index处的第一个不匹配字符小于第二个字符串在index处的字符,那么我们将返回 1。
- 在 for 循环结束后,如果两个字符串的长度相等则返回 0。如果第一个字符串的长度小于第二个字符串的长度,则返回 -1,否则返回 1。
- 通过传递要比较的字符串作为参数从主函数调用Compare()函数。如果Compare()函数返回 0 则打印第一个字符串与第二个字符串相同。如果Compare()函数返回 -1 则打印第一个字符串小于第二个字符串,否则打印第一个字符串大于第二个字符串。
例子:
C#
// C# program to illustrate the working of
// custom Compare() method to compare two strings
using System;
class GFG{
// Compare method to compare two strings
static public int Compare(string myString1, string myString2)
{
// len stores minimum of two string lengths
int len = Math.Min(myString1.Length, myString2.Length);
// Iterate over all characters
for(int index = 0; index < len; index++)
{
// If the first not matched character of first
// string is smaller than the second string then
// return -1
if (myString1[index] < myString2[index])
return -1;
// If the first not matched character of first
// string is greater than the second string then
// return 1
else if (myString1[index] > myString2[index])
return 1;
}
// If lengths are equal
// Return 0
if (myString1.Length == myString2.Length)
return 0;
// If length of first string is smaller than the second string
// then return -1
// If length of first string is greater than the second string
// then return 1
return ((myString1.Length < myString2.Length) ? -1 : 1);
}
// Driver code
static public void Main()
{
// Initialize a string
string myString1 = "GeeksforGeeks";
// Initialize another string
string myString2 = "Geeks";
// Initialize another string
string myString3 = "GeeksforGeeks";
// If value returned by this method is equal to 0
// Then print both strings are same
if (Compare(myString1, myString2) == 0)
Console.WriteLine($"{myString1} and {myString2} are same.");
// If value returned by this method is smaller than 0
// Then print the first string is smaller than the second string
else if (Compare(myString1, myString3) < 0)
Console.WriteLine(
$"{myString1} is lexicographically smaller than {myString2}.");
// If value returned by this method is greater than 0
// Then print the first string is greater than the second string
else
Console.WriteLine(
$"{myString1} is lexicographically greater than {myString2}.");
// If value returned by this method is equal to 0
// Then print both strings are same
if (Compare(myString1, myString3) == 0)
Console.WriteLine($"{myString1} and {myString3} are same.");
// If value returned by this method is smaller than 0
// Then print the first string is smaller than the second string
else if (Compare(myString1, myString3) < 0)
Console.WriteLine(
$"{myString1} is lexicographically smaller than {myString3}.");
// If value returned by this method is greater than 0
// Then print the first string is greater than the second string
else
Console.WriteLine(
$"{myString1} is lexicographically greater than {myString3}.");
// If value returned by this method is equal to 0
// Then print both strings are same
if (Compare(myString2, myString3) == 0)
Console.WriteLine($"{myString2} and {myString3} are same.");
// If value returned by this method is smaller than 0
// Then print the first string is smaller than the second string
else if (Compare(myString2, myString3) < 0)
Console.WriteLine(
$"{myString2} is lexicographically smaller than {myString3}.");
// If value returned by this method is greater than 0
// Then print the first string is greater than the second string
else
Console.WriteLine(
$"{myString2} is lexicographically greater than {myString3}.");
}
}
GeeksforGeeks is lexicographically greater than Geeks.
GeeksforGeeks and GeeksforGeeks are same.
Geeks is lexicographically smaller than GeeksforGeeks.