C# 检测字符串是否全部为大写
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/448206/
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
Detecting if a string is all CAPS
提问by StubbornMule
In C# is there a way to detect if a string is all caps?
在 C# 中有没有办法检测字符串是否全部大写?
Most of the strings will be short(ie under 100 characters)
大多数字符串都很短(即少于 100 个字符)
采纳答案by Greg Dean
No need to create a new string:
无需创建新字符串:
bool IsAllUpper(string input)
{
for (int i = 0; i < input.Length; i++)
{
if (!Char.IsUpper(input[i]))
return false;
}
return true;
}
Edit:If you want to skip non-alphabetic characters (The OP's original implementation does not, but his/her comments indicate that they might want to) :
编辑:如果您想跳过非字母字符(OP 的原始实现没有,但他/她的评论表明他们可能想要):
bool IsAllUpper(string input)
{
for (int i = 0; i < input.Length; i++)
{
if (Char.IsLetter(input[i]) && !Char.IsUpper(input[i]))
return false;
}
return true;
}
回答by Nick
I would convert the string to all caps (with ToUpper
) then compare that to the original (using Equals
). Should be doable in one line of code.
我会将字符串转换为全部大写(使用ToUpper
),然后将其与原始字符串(使用)进行比较Equals
。在一行代码中应该是可行的。
return s.Equals(s.ToUpper())
return s.Equals(s.ToUpper())
回答by BoltBait
Simple?
简单的?
if (input.ToUpper() == input)
{
// string is all upper
}
回答by M4N
Use
用
if (input == input.ToUpper())
回答by Joe
If this needs to have good perf, I'm assuming it happens a lot. If so, take your solution and do it a few million times and time it. I suspect what you've got is better than the other solutions because you aren't creating a new garbage collected object that has to be cleaned up, and you can't make a copy of a string without iterating over it anyways.
如果这需要有良好的性能,我假设它发生了很多。如果是这样,请采用您的解决方案并执行几百万次并计时。我怀疑你得到的比其他解决方案更好,因为你没有创建一个必须清理的新垃圾收集对象,而且你不能在不迭代的情况下复制字符串。
回答by Leandro López
I think the following:
我认为以下几点:
bool equals = (String.Compare(input, input.ToUpper(), StringComparison.Ordinal) == 0)
Will work also, and you can make sure that the comparison is made without taking into account the string casing (I think VB.NET ignores case by default). O even use String.CompareOrdinal(input, input.ToUpper())
.
也可以工作,并且您可以确保在不考虑字符串大小写的情况下进行比较(我认为 VB.NET 默认忽略大小写)。O 甚至使用String.CompareOrdinal(input, input.ToUpper())
.
回答by Jon Skeet
I like the LINQ approach.
我喜欢 LINQ 方法。
If you want to restrict it to all upper case letters(i.e. no spaces etc):
如果您想将其限制为所有大写字母(即没有空格等):
return input.All(c => char.IsUpper(c));
or using a method group conversion:
或使用方法组转换:
return input.All(char.IsUpper);
If you want to just forbid lower case letters:
如果您只想禁止小写字母:
return !input.Any(c => char.IsLower(c));
or
或者
return !input.Any(char.IsLower);
回答by PEZ
Regular expressions comes to mind. Found this out there: http://en.csharp-online.net/Check_if_all_upper_case_string
正则表达式浮现在脑海中。在那里找到了这个:http: //en.csharp-online.net/Check_if_all_upper_case_string
回答by Ifeanyi Echeruo
Make sure your definition of capitalization matches .Nets definition of capitalization.
确保您对大写的定义与 .Nets 对大写的定义相匹配。
ToUpper() in .Net is a linguistic operation. In some languages capitalization rules are not straight forward. Turkish I is famous for this.
.Net 中的 ToUpper() 是一种语言操作。在某些语言中,大小写规则并不简单。土耳其语 I 因这一点而闻名。
// Meaning of ToUpper is linguistic and depends on what locale this executes
// This test could pass or fail in ways that surprise you.
if (input.ToUpper() == input)
{
// string is all upper
}
You could use
你可以用
// Meaning of ToUpper is basically 'ASCII' ToUpper no matter the locale.
if (input.ToUpper(CultureInfo.InvariantCulture) == input)
{
// string is all upper
}
You may be tempted to save memory doing character by character capitalization
您可能会想通过字符大写来节省内存
for(int i = 0; i < input.Length; i++) {
if(input[i] != Char.ToUpper(input[i], CultureInfo.InvariantCulture)) {
return false;
}
}
The above code introduces a bug. Some non English 'letters' require two .net characters to encode (a surrogate pair). You have to detect these pairs and capitalize them as a single unit.
上面的代码引入了一个错误。一些非英语“字母”需要两个 .net 字符进行编码(代理对)。您必须检测这些对并将它们大写为一个单元。
Also if you omit the culture info to get linguistic capitalization you are introducing a bug where in some locales your home brew capitalization algorithm disagrees with the the .net algorithm for that locale.
此外,如果您省略文化信息以获取语言大写,则会引入一个错误,即在某些语言环境中,您的自制啤酒大写算法与该语言环境的 .net 算法不一致。
Of course none of this matters if your code will never run outside English speaking locales or never receive non English text.
当然,如果您的代码永远不会在讲英语的语言环境之外运行或永远不会收到非英语文本,那么这一切都不重要。
回答by Eduardo Miranda
Another approach
另一种方法
return input.Equals(input.ToUpper(), StringComparison.Ordinal)