C# 如何比较两个字符串及其大小写符号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18378448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to compare two strings and their upper and lower case signs
提问by daidai
Let's say I have 2 strings.First string is x = "abc" and the second one is y = "ABC".When I write in c# following code:
假设我有 2 个字符串。第一个字符串是 x = "abc",第二个是 y = "ABC"。当我用 c# 编写以下代码时:
if(x == y)
or
或者
if(x.Equals(y))
return value is true.How can I check their upper and lower case?
返回值为true。如何检查它们的大小写?
回答by Pleun
Try:
尝试:
Case sensitive:
区分大小写:
String.Equals (a,b)
Case Insensitive
不区分大小写
string.Equals(a, b, StringComparison.CurrentCultureIgnoreCase);
回答by Tim Schmelter
The return value is nottrue
but false
since .NET is case sensitive by default.
返回值不是true
,false
因为 .NET 默认区分大小写。
From String.Equals
:
This method performs an ordinal (case-sensitiveand culture-insensitive) comparison.
此方法执行顺序(区分大小写和不区分区域性)比较。
For ==
the same is true since String.Equality
operatorcalls Equals
:
对于==
相同的,因为是真实的String.Equality
运营商电话Equals
:
This operator is implemented using the Equalsmethod, which means the comparands are tested for a combination of reference and value equality. This operator performs an ordinal comparison.
此运算符是使用Equals方法实现的,这意味着比较对象会针对引用和值相等性的组合进行测试。此运算符执行序数比较。
This will compare case insensitively:
这将不区分大小写进行比较:
bool equals = x.Equals(y , StringComparison.OrdinalIgnoreCase);
If you just want to know if a character is upper or lower case you can use these methods:
如果您只想知道字符是大写还是小写,可以使用以下方法:
bool isUpperChar = Char.IsUpper("ABC"[0]); // yes
bool isLowerChar = Char.IsLower("ABC"[0]); // no
回答by xanatos
As Pleun wrote, or you can
正如 Pleun 所写,或者你可以
StringComparer.CurrentCultureIgnoreCase.Equals(a, b)
Note that we are using the CurrentCulture
ordering method. Sometimes you'll have to use different ordering methods (every language orders the letters in a different way)
请注意,我们使用的是CurrentCulture
订购方法。有时您必须使用不同的排序方法(每种语言以不同的方式对字母进行排序)
If you are sure that you are only ordering ASCII characters then
如果您确定您只订购 ASCII 字符,那么
StringComparer.OrdinalIgnoreCase.Equals(a, b)
is a little faster (or in general methods where you can select the OrdinalIgnoreCase
)
快一点(或在一般方法中,您可以选择OrdinalIgnoreCase
)
In general converting ToUpper()
or ToLower()
two strings to compare them is wrong(and slow, because you have to convert them fully before comparing them, while perhaps they are different in the first character)... Wrong because in Turkish there are four i
一般来说,转换ToUpper()
或ToLower()
两个字符串来比较它们是错误的(而且很慢,因为你必须在比较它们之前完全转换它们,而它们的第一个字符可能不同)......错误,因为在土耳其语中有四个i
http://codeblog.jonskeet.uk/2009/11/02/omg-ponies-aka-humanity-epic-fail/
http://codeblog.jonskeet.uk/2009/11/02/omg-ponies-aka-humanity-epic-fail/
回答by Dmitry Bychenko
First, you should decide whether you compare strings in culture dependent
or independentway (e.g. in Russian Culture letters "E" and "Ё" often treats as being the same; Finnish tends to treat "V" and "W" as being the same etc.). Next you should choose whether use or not use case("a" v. "A"). So there're 6 possible comparisons:
首先,您应该决定是否以culture dependent
或独立的方式比较字符串(例如,在俄罗斯文化中,字母“E”和“Ё”通常被视为相同;芬兰语倾向于将“V”和“W”视为相同等。 )。接下来您应该选择是否使用用例(“a”v.“A”)。所以有 6 种可能的比较:
Ordinal (culture independent) comparisons:
序数(与文化无关)比较:
// Ignore case comparison
Boolean equals = String.Equals(x, y, StringComparison.OrdinalIgnoreCase);
// Case comparison
Boolean equals = String.Equals(x, y, StringComparison.Ordinal);
Current culture comparisons:
当前文化比较:
// Current culture, ignore case comparison
Boolean equals = String.Equals(x, y, StringComparison.CurrentCulture);
// Current culture, case comparison
Boolean equals = String.Equals(x, y, StringComparison.CurrentCultureIgnoreCase);
Explicit culture comparisons:
显式文化比较:
CultureInfo culture = new CultureInfo("Ru-ru"); // <- Or whatever you want
// Explicit culture, ignore case comparison
Boolean equals = culture.CompareInfo.Compare(x, y, CompareOptions.IgnoreCase);
// Explicit culture, case comparison
Boolean equals = culture.CompareInfo.Compare(x, y, CompareOptions.None);
回答by Krishna Sarma
This is another option you can try.
这是您可以尝试的另一种选择。
if(string.Compare("a", "A", true) == 0)