首页  >  文章  >  后端开发  >  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# User Control下一篇:C# Base