是否有现有的库方法可以检查 Java 中的 String 是全部大写还是小写?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/677561/
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
Is there an existing library method that checks if a String is all upper case or lower case in Java?
提问by Nicolai
I know there are plenty of upper()
methods in Java and other frameworks like Apache commons lang, which convert a String to all upper case.
我知道upper()
Java 和其他框架(如 Apache commons lang)中有很多方法可以将字符串转换为全部大写。
Are there any common libraries that provide a method like isUpper(String s)
and isLower(String s)
, to check if all the characters in the String are upper or lower case?
是否有任何公共库提供像isUpper(String s)
and这样的方法isLower(String s)
来检查 String 中的所有字符是大写还是小写?
EDIT:
编辑:
Many good answers about converting to Upper and comparing to this. I guess I should have been a bit more specific, and said that I already had thought of that, but I was hoping to be able to use an existing method for this.
关于转换为 Upper 并与此进行比较的许多很好的答案。我想我应该更具体一点,并说我已经想到了这一点,但我希望能够为此使用现有的方法。
Good comment about possible inclusion of this in apache.commons.lang.StringUtils. Someone has even submitted a patch (20090310). Hopefully we will see this soon. https://issues.apache.org/jira/browse/LANG-471
关于可能将其包含在 apache.commons.lang.StringUtils 中的好评论。甚至有人提交了补丁(20090310)。希望我们很快就会看到这一点。 https://issues.apache.org/jira/browse/LANG-471
EDIT:
编辑:
What I needed this method for, was to capitalize names of hotels that sometimes came in all uppercase. I only wanted to capitalize them if they were all lower or upper case. I did run in to the problems with non letter chars mentioned in some of the posts, and ended up doing something like this:
我需要这种方法的目的是将有时全部大写的酒店名称大写。如果它们都是小写或大写,我只想将它们大写。我确实遇到了一些帖子中提到的非字母字符的问题,最终做了这样的事情:
private static boolean isAllUpper(String s) {
for(char c : s.toCharArray()) {
if(Character.isLetter(c) && Character.isLowerCase(c)) {
return false;
}
}
return true;
}
This discussion and differing solutions (with different problems), clearly shows that there is a need for a good solid isAllUpper(String s) method in commons.lang
这个讨论和不同的解决方案(有不同的问题),清楚地表明在 commons.lang 中需要一个好的固体 isAllUpper(String s) 方法
Until then I guess that the myString.toUpperCase().equals(myString)
is the best way to go.
在那之前,我想这myString.toUpperCase().equals(myString)
是最好的方法。
采纳答案by MikeFHay
Guava's CharMatcherstend to offer very expressive and efficient solutions to this kind of problem.
Guava 的CharMatchers倾向于为此类问题提供非常有表现力和高效的解决方案。
CharMatcher.javaUpperCase().matchesAllOf("AAA"); // true
CharMatcher.javaUpperCase().matchesAllOf("A SENTENCE"); // false
CharMatcher.javaUpperCase().or(CharMatcher.whitespace()).matchesAllOf("A SENTENCE"); // true
CharMatcher.javaUpperCase().or(CharMatcher.javaLetter().negate()).matchesAllOf("A SENTENCE"); // true
CharMatcher.javaLowerCase().matchesNoneOf("A SENTENCE"); // true
A static import for com.google.common.base.CharMatcher.*
can help make these more succinct.
静态导入com.google.common.base.CharMatcher.*
可以帮助使这些更简洁。
javaLowerCase().matchesNoneOf("A SENTENCE"); // true
回答by Markus Lausberg
Not that i know.
不是我知道的。
You can copy the string and convert the copy to lower/upper case and compare to the original one.
您可以复制字符串并将副本转换为小写/大写并与原始字符串进行比较。
Or create a loop which checks the single characters if the are lower or upper case.
或者创建一个循环来检查单个字符是小写还是大写。
回答by Outlaw Programmer
This method might be faster than comparing a String to its upper-case version as it requires only 1 pass:
此方法可能比将 String 与其大写版本进行比较更快,因为它只需要 1 次传递:
public static boolean isUpper(String s)
{
for(char c : s.toCharArray())
{
if(! Character.isUpperCase(c))
return false;
}
return true;
}
Please note that there might be some localization issues with different character sets. I don't have any first hand experience but I think there are some languages (like Turkish) where different lower case letters can map to the same upper case letter.
请注意,不同的字符集可能存在一些本地化问题。我没有任何第一手经验,但我认为有些语言(如土耳其语)可以将不同的小写字母映射到相同的大写字母。
回答by SmuK
You can use java.lang.Character.isUpperCase() Then you can easily write a method that check if your string is uppercase (with a simple loop).
您可以使用 java.lang.Character.isUpperCase() 然后您可以轻松编写一个方法来检查您的字符串是否为大写(使用一个简单的循环)。
Sending the message toUpperCase() to your string and then checking if the result is equal to your string will be probably slower.
将消息 toUpperCase() 发送到您的字符串,然后检查结果是否等于您的字符串可能会更慢。
回答by Simon Nickerson
Not a library function unfortunately, but it's fairly easy to roll your own. If efficiency is a concern, this might be faster than s.toUpperCase().equals(s) because it can bail out early.
不幸的是,这不是一个库函数,但推出自己的函数相当容易。如果考虑效率,这可能比 s.toUpperCase().equals(s) 更快,因为它可以提前退出。
public static boolean isUpperCase(String s)
{
for (int i=0; i<s.length(); i++)
{
if (!Character.isUpperCase(s.charAt(i)))
{
return false;
}
}
return true;
}
Edit:As other posters and commenters have noted, we need to consider the behaviour when the string contains non-letter characters: should isUpperCase("HELLO1") return true or false? The function above will return false because '1' is not an upper case character, but this is possibly not the behaviour you want. An alternative definition which would return true in this case would be:
编辑:正如其他海报和评论者所指出的,我们需要考虑字符串包含非字母字符时的行为:isUpperCase("HELLO1") 应该返回 true 还是 false?上面的函数将返回 false,因为 '1' 不是大写字符,但这可能不是您想要的行为。在这种情况下返回 true 的替代定义是:
public static boolean isUpperCase2(String s)
{
for (int i=0; i<s.length(); i++)
{
if (Character.isLowerCase(s.charAt(i)))
{
return false;
}
}
return true;
}
回答by Simon Nickerson
Try this, may help.
试试这个,可能会有所帮助。
import java.util.regex.Pattern;
private static final String regex ="^[A-Z0-9]"; //alpha-numeric uppercase
public static boolean isUpperCase(String str){
return Pattern.compile(regex).matcher(str).find();
}
with this code, we just change the regex.
使用此代码,我们只需更改正则表达式。
回答by gerardw
Now in StringUtils isAllUpperCase
现在在 StringUtils isAllUpperCase
回答by Kunal
This if condition can get the expected result:
这个 if 条件可以得到预期的结果:
String input = "ANYINPUT";
if(input.equals(input.toUpperCase())
{
// input is all upper case
}
else if (input.equals(input.toLowerCase())
{
// input is all lower case
}
else
{
// input is mixed case
}