在C#中比较两个字符串可以使用以下方法:
string str1 = "Hello";
string str2 = "World";
if (str1.Equals(str2))
{
Console.WriteLine("The strings are equal.");
}
else
{
Console.WriteLine("The strings are not equal.");
}
string str1 = "Hello";
string str2 = "World";
if (str1 == str2)
{
Console.WriteLine("The strings are equal.");
}
else
{
Console.WriteLine("The strings are not equal.");
}
string str1 = "Hello";
string str2 = "World";
int result = string.Compare(str1, str2);
if (result == 0)
{
Console.WriteLine("The strings are equal.");
}
else if (result < 0)
{
Console.WriteLine("str1 is less than str2.");
}
else
{
Console.WriteLine("str1 is greater than str2.");
}
以上是在C#中比较两个字符串的常见方法,根据具体情况选择适合的方法进行比较。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:怎么比较两个字符串c语言