比较两个字符串值 [英] compare two string value
问题描述
我想比较两个字符串值,就像这样:
I'd like to compare two string values, like this:
if (lblCapacity.Text <= lblSizeFile.Text)
我该怎么办?
推荐答案
我假设您正在比较词典的字符串顺序,在这种情况下,您可以使用静态方法String.Compare。
I'm assuming that you are comparing strings in lexicographical order, in which case you can use the Static method String.Compare.
例如,您有两个字符串str1和str2,并且您想查看str1是否在字母表中位于str2之前。您的代码如下所示:
For example, you have two strings str1 and str2, and you want to see if str1 comes before str2 in an alphabet. Your code would look like this :
string str1 = "A string";
string str2 = "Some other string";
if(String.Compare(str1,str2) < 0)
{
// str1 is less than str2
Console.WriteLine("Yes");
}
else if(String.Compare(str1,str2) == 0)
{
// str1 equals str2
Console.WriteLine("Equals");
}
else
{
// str11 is greater than str2, and String.Compare returned a value greater than 0
Console.WriteLine("No");
}
上面的代码将返回yes。 String.Compare有许多重载版本,包括一些您可以忽略大小写或使用格式字符串的版本。查看 String.Compare 。
This above code would return yes. There are many overloaded versions of String.Compare, including some where you can ignore case, or use format strings. Check out String.Compare.
这篇关于比较两个字符串值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!