如何在java中检查空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3745421/
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 check empty spaces in java
提问by user441978
String selectedVal = "";
for (SelectItem item : filterItems) {
selectedVal = item.getValue().toString();
break;
}
I am getting selectedVal=" " how to check this empty space in java.
我得到了 selectedVal="" 如何在 java 中检查这个空白空间。
I tried with if(!selectedVal.equals("") and if(!selectedVal.isEmpty()) but condition getting true.How to check more than one empty space ?
我试过 if(!selectedVal.equals("") 和 if(!selectedVal.isEmpty()) 但条件为真。如何检查多个空格?
回答by Shimmy Weitzhandler
I am not a java programmer, but with regex, \smeans white space.
我不是 Java 程序员,但对于正则表达式,\s表示空格。
Thislink might also be useful:
此链接也可能有用:
/**
* Helper function for making null strings safe for comparisons, etc.
*
* @return (s == null) ? "" : s;
*/
public static String makeSafe(String s) {
return (s == null) ? "" : s;
}
/**
* Helper function for null, empty, and whitespace string testing.
*
* @return true if s == null or s.equals("") or s contains only whitespace
* characters.
*/
public static boolean isEmptyOrWhitespace(String s) {
s = makeSafe(s);
for (int i = 0, n = s.length(); i < n; i++) {
if (!Character.isWhitespace(s.charAt(i))) {
return false;
}
}
return true;
}
回答by Colin Hebert
You can trim()
your string before checking with isEmpty()
trim()
在检查之前,您可以使用您的字符串isEmpty()
boolean isEmpty = myString.trim().isEmpty();
Beware of isEmpty()
, it's only available since Java SE 6
当心isEmpty()
,它仅从 Java SE 6 开始可用
Resources :
资源 :
回答by Grodriguez
I use this all the time:
我经常用这个:
public static boolean isBlank(String s)
{
return (s == null) || (s.trim().length() == 0);
}
Returns true
on null, empty string, or whitespace only.
仅返回true
null、空字符串或空格。
回答by Benoit Courtine
For such a simple test, using an external library is not a good idea, but if you need String manipulation (left and right padding, etc.), you can go for Apache commons langand the StringUtils.isEmpty()method.
对于这样一个简单的测试,使用外部库不是一个好主意,但是如果您需要字符串操作(左右填充等),您可以使用Apache commons lang和 StringUtils.isEmpty()方法。
More recently, you can use the Google Guavalibrary and the class Strings. This library has a lot of usefull util methods (null handling, etc.). Once again, use this library only if you have others needs than checking empty strings.
最近,您可以使用Google Guava库和Strings类。这个库有很多有用的 util 方法(空处理等)。再一次,仅当您有其他需要而不是检查空字符串时才使用此库。
回答by Tony Ennis
I use a routine similar to what Grodriguez posted. It ends up in a util/BagOTricks.java
file in every project. My routine does a similar check and returns a null as a space, or the trimmed input string.
我使用类似于 Grodriguez 发布的程序。它最终util/BagOTricks.java
在每个项目中的一个文件中。我的例程进行了类似的检查,并返回空值作为空格或修剪后的输入字符串。
回答by jcrshankar
boolean isEmpty = myString.toString().trim().isEmpty()
boolean isEmpty = myString.toString().trim().isEmpty()
回答by dkkd
You can use this method:
您可以使用此方法:
public boolean isNullOrEmpty(String s) {
return s == null || s.trim().isEmpty();
}
Method returns true when passed string is null or empty / only contains blank spaces.
当传递的字符串为空或空/仅包含空格时,方法返回 true。
回答by Toby Peschel
Using the Apache Commons Lang library, you can use StringUtils.isEmpty()
to check if a String is empty ("") or null, and StringUtils.isBlank()
to check if a String is whitespace, empty ("") or null.
使用 Apache Commons Lang 库,您可以StringUtils.isEmpty()
检查字符串是否为空 ("") 或 null,以及StringUtils.isBlank()
检查字符串是否为空格、空 ("") 或 null。
Note the differences:
注意区别:
isEmpty()
是空的()
StringUtils.isEmpty(null) = true
StringUtils.isEmpty("") = true
StringUtils.isEmpty(" ") = false
StringUtils.isEmpty("bob") = false
StringUtils.isEmpty(" bob ") = false
isBlank()
isBlank()
StringUtils.isBlank(null) = true
StringUtils.isBlank("") = true
StringUtils.isBlank(" ") = true
StringUtils.isBlank("bob") = false
StringUtils.isBlank(" bob ") = false