How do I check that a Java String is not all whitespaces?
I want to check that Java String or character array is not just made up of whitespaces, using Java?
This is a very similar question except it's Javascript:
How can I check if string contains characters & whitespace, not just whitespace?
EDIT: I removed the bit about alphanumeric characters, so it makes more sense.
java string whitespace
add a comment |
I want to check that Java String or character array is not just made up of whitespaces, using Java?
This is a very similar question except it's Javascript:
How can I check if string contains characters & whitespace, not just whitespace?
EDIT: I removed the bit about alphanumeric characters, so it makes more sense.
java string whitespace
3
Be aware that there are many different definitions of whitespace: spreadsheets.google.com/pub?key=pd8dAQyHbdewRsnE5x5GzKQ Which do you want? Or then you say "has an alphanumeric character", which is a completely different thing. Please clarify.
– Kevin Bourrillion
Jul 14 '10 at 15:20
Apologies for the confusion ... not all whitespaces is the key- basically if it has all whitespace characters I want to exclude it, because it has no content.
– Ankur
Jul 14 '10 at 15:27
With JDK/11 you can make use of theString.isBlankAPI for the same.
– nullpointer
May 31 '18 at 19:01
add a comment |
I want to check that Java String or character array is not just made up of whitespaces, using Java?
This is a very similar question except it's Javascript:
How can I check if string contains characters & whitespace, not just whitespace?
EDIT: I removed the bit about alphanumeric characters, so it makes more sense.
java string whitespace
I want to check that Java String or character array is not just made up of whitespaces, using Java?
This is a very similar question except it's Javascript:
How can I check if string contains characters & whitespace, not just whitespace?
EDIT: I removed the bit about alphanumeric characters, so it makes more sense.
java string whitespace
java string whitespace
edited Jul 15 '18 at 7:29
nullpointer
46.5k1199190
46.5k1199190
asked Jul 14 '10 at 14:23
AnkurAnkur
21k96219303
21k96219303
3
Be aware that there are many different definitions of whitespace: spreadsheets.google.com/pub?key=pd8dAQyHbdewRsnE5x5GzKQ Which do you want? Or then you say "has an alphanumeric character", which is a completely different thing. Please clarify.
– Kevin Bourrillion
Jul 14 '10 at 15:20
Apologies for the confusion ... not all whitespaces is the key- basically if it has all whitespace characters I want to exclude it, because it has no content.
– Ankur
Jul 14 '10 at 15:27
With JDK/11 you can make use of theString.isBlankAPI for the same.
– nullpointer
May 31 '18 at 19:01
add a comment |
3
Be aware that there are many different definitions of whitespace: spreadsheets.google.com/pub?key=pd8dAQyHbdewRsnE5x5GzKQ Which do you want? Or then you say "has an alphanumeric character", which is a completely different thing. Please clarify.
– Kevin Bourrillion
Jul 14 '10 at 15:20
Apologies for the confusion ... not all whitespaces is the key- basically if it has all whitespace characters I want to exclude it, because it has no content.
– Ankur
Jul 14 '10 at 15:27
With JDK/11 you can make use of theString.isBlankAPI for the same.
– nullpointer
May 31 '18 at 19:01
3
3
Be aware that there are many different definitions of whitespace: spreadsheets.google.com/pub?key=pd8dAQyHbdewRsnE5x5GzKQ Which do you want? Or then you say "has an alphanumeric character", which is a completely different thing. Please clarify.
– Kevin Bourrillion
Jul 14 '10 at 15:20
Be aware that there are many different definitions of whitespace: spreadsheets.google.com/pub?key=pd8dAQyHbdewRsnE5x5GzKQ Which do you want? Or then you say "has an alphanumeric character", which is a completely different thing. Please clarify.
– Kevin Bourrillion
Jul 14 '10 at 15:20
Apologies for the confusion ... not all whitespaces is the key- basically if it has all whitespace characters I want to exclude it, because it has no content.
– Ankur
Jul 14 '10 at 15:27
Apologies for the confusion ... not all whitespaces is the key- basically if it has all whitespace characters I want to exclude it, because it has no content.
– Ankur
Jul 14 '10 at 15:27
With JDK/11 you can make use of the
String.isBlank API for the same.– nullpointer
May 31 '18 at 19:01
With JDK/11 you can make use of the
String.isBlank API for the same.– nullpointer
May 31 '18 at 19:01
add a comment |
13 Answers
13
active
oldest
votes
Shortest solution I can think of:
if (string.trim().length() > 0) ...
This only checks for (non) white space. If you want to check for particular character classes, you need to use the mighty match() with a regexp such as:
if (string.matches(".*\w.*")) ...
...which checks for at least one (ASCII) alphanumeric character.
7
FWIW: I would expect the first solution to be considerably faster.
– Stephen C
Jul 14 '10 at 14:29
2
@Stephen C: Absolutely! But as @Uri pointed out, I'm having to solve two different problems thanks to the ambiguity of the question :) Also, I rarely usematches(): for performance, I usually store thePatternin afinal static. Pays off if the same code runs frequently.
– Carl Smotricz
Jul 14 '10 at 14:31
2
@Andreas_D: Heh, I got my orders! The OP said he wanted to check a string or char array, he never said anything about nulls! :) *checks the fine print in the contract* "nullis not a string!"
– Carl Smotricz
Jul 14 '10 at 15:01
1
Also, "\w" only matches a limited subset of non-whitespace characters rather than all non-whitespace since it refers to "word characters" defined as A-Z, a-z, 0-9, and underscore.
– Rob Raisch
May 28 '11 at 15:30
1
@Rob Raisch, I must object to both of your comments. Second comment first: I explicitly gave "w" as an example checking for a particular character class, not to solve the original simpler problem. First comment: No, "\w" will not match a series of word characters preceded, surrounded or followed by other characters, e.g. whitespace. String.matches() matches the entire string, not a subset.
– Carl Smotricz
Dec 4 '11 at 11:51
|
show 3 more comments
I would use the Apache Commons Lang library. It has a class called StringUtils that is useful for all sorts of String operations. For checking if a String is not all whitespaces, you can use the following:
StringUtils.isBlank(<your string>)
Here is the reference: StringUtils.isBlank
8
I prefer this solution compared to using the chosen answer. This will also check for string == null
– Richard
Jul 14 '10 at 16:57
This is now incorrect.StringUtils.isEmptywill now return false if you pass in " ".
– James Spence
May 30 '17 at 20:25
add a comment |
Slightly shorter than what was mentioned by Carl Smotricz:
!string.trim().isEmpty();
7
You young whippersnappers and your newfangled post-Java-1.6 trickery! Seriously, at least one project in my company still runs on Java 1.4 (sigh).
– Carl Smotricz
Dec 4 '11 at 11:54
Shorter? Yes. Personally, I like the more verbose coding style
– Michel
Mar 12 '14 at 11:34
add a comment |
StringUtils.isBlank(CharSequence)
https://commons.apache.org/proper/commons-lang/javadocs/api-release/org/apache/commons/lang3/StringUtils.html#isBlank-java.lang.CharSequence-
Hmm, my compiler doesn't recognize this method in StringUtils.
– Scott Biggs
Jan 17 at 21:22
add a comment |
if(target.matches("\S"))
// then string contains at least one non-whitespace character
Note use of back-slash cap-S, meaning "non-whitespace char"
I'd wager this is the simplest (and perhaps the fastest?) solution.
1
Let try:String year="1995"; year.matches("\S"); will return falseSo this is not correct solution. :|
– Nhat Dinh
May 20 '15 at 2:59
6
Nhat, you are correct though I'm at a loss to explain why. According to the Java docs, String.matches checks to see if a given string matches a regex. A little experimentation shows that this is not entirely accurate, as this function appears to match ONLY if the provided regex matches the ENTIRE string! So, changing the regex above ("\S") to "^.*\S.*$" will work as expected, though this behavior isn't correctly documented and appears to diverge substantially from every other implementation of string matching using Regular Expressions.
– Rob Raisch
May 26 '15 at 15:46
add a comment |
This answer focusses more on the sidenote "i.e. has at least one alphanumeric character". Besides that, it doesn't add too much to the other (earlier) solution, except that it doesn't hurt you with NPE in case the String is null.
We want false if (1) s is null or (2) s is empty or (3) s only contains whitechars.
public static boolean containsNonWhitespaceChar(String s) "".equals(s.trim()));
add a comment |
If you are only checking for whitespace and don't care about null then you can use org.apache.commons.lang.StringUtils.isWhitespace(String str),
StringUtils.isWhitespace(String str);
(Checks if the String contains only whitespace.)
If you also want to check for null(including whitespace) then
StringUtils.isBlank(String str);
add a comment |
With JDK/11, now you can make use of the String.isBlank API to check if the given string is not all made up of
String str1 = " ";
System.out.println(str1.isBlank()); // made up of all whitespaces, prints true
String str2 = " a";
System.out.println(str2.isBlank()); // prints false
The javadoc for the same is :
/**
* Returns @code true if the string is empty or contains only
* @link Character#isWhitespace(int) white space codepoints,
* otherwise @code false.
*
* @return @code true if the string is empty or contains only
* @link Character#isWhitespace(int) white space codepoints,
* otherwise @code false
*
* @since 11
*/
public boolean isBlank()
add a comment |
The trim method should work great for you.
http://download.oracle.com/docs/cd/E17476_01/javase/1.4.2/docs/api/java/lang/String.html#trim()
Returns a copy of the string, with
leading and trailing whitespace
omitted. If this String object
represents an empty character
sequence, or the first and last
characters of character sequence
represented by this String object both
have codes greater than 'u0020' (the
space character), then a reference to
this String object is returned.
Otherwise, if there is no character
with a code greater than 'u0020' in
the string, then a new String object
representing an empty string is
created and returned.
Otherwise, let k be the index of the
first character in the string whose
code is greater than 'u0020', and let
m be the index of the last character
in the string whose code is greater
than 'u0020'. A new String object is
created, representing the substring of
this string that begins with the
character at index k and ends with the
character at index m-that is, the
result of this.substring(k, m+1).
This method may be used to trim
whitespace from the beginning and end
of a string; in fact, it trims all
ASCII control characters as well.
Returns: A copy of this string with
leading and trailing white space
removed, or this string if it has no
leading or trailing white space.leading or trailing white space.
You could trim and then compare to an empty string or possibly check the length for 0.
Link in answer is dead - 404 | We're sorry, the page does not exist or is no longer available.
– Pang
Jan 27 '18 at 6:35
add a comment |
If you are using Java 11, the new isBlank string method will come in handy:
!s.isBlank();
If you are using Java 8, 9 or 10, you could build a simple stream to check that a string is not whitespace only:
!s.chars().allMatch(Character::isWhitespace));
In addition to not requiring any third-party libraries such as Apache Commons Lang, these solutions have the advantage of handling any white space character, and not just plain ' ' spaces as would a trim-based solution suggested in many other answers. You can refer to the Javadocs for an exhaustive list of all supported white space types. Note that empty strings are also covered in both cases.
add a comment |
Alternative:
boolean isWhiteSpaces( String s )
return s != null && s.matches("\s+");
1
\s* will match all strings with or without whitespaces. Perhaps you mean \s+ ?
– Rob Raisch
May 27 '11 at 22:11
add a comment |
trim() and other mentioned regular expression do not work for all types of whitespaces
i.e: Unicode Character 'LINE SEPARATOR' http://www.fileformat.info/info/unicode/char/2028/index.htm
Java functions Character.isWhitespace() covers all situations.
That is why already mentioned solution
StringUtils.isWhitespace( String ) /or StringUtils.isBlank(String)
should be used.
add a comment |
StringUtils.isEmptyOrWhitespaceOnly(<your string>)
will check :
- is it null
- is it only space
- is it empty string ""
https://www.programcreek.com/java-api-examples/?class=com.mysql.jdbc.StringUtils&method=isEmptyOrWhitespaceOnly
Is this from a library? Link to the project. Or built into Java? Indicate the package.
– Basil Bourque
Dec 24 '18 at 7:59
@BasilBourque i think this is com.mysql.jdbc.StringUtils.isEmptyOrWhitespaceOnly
– ahmet_y
Dec 24 '18 at 9:31
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3247067%2fhow-do-i-check-that-a-java-string-is-not-all-whitespaces%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
13 Answers
13
active
oldest
votes
13 Answers
13
active
oldest
votes
active
oldest
votes
active
oldest
votes
Shortest solution I can think of:
if (string.trim().length() > 0) ...
This only checks for (non) white space. If you want to check for particular character classes, you need to use the mighty match() with a regexp such as:
if (string.matches(".*\w.*")) ...
...which checks for at least one (ASCII) alphanumeric character.
7
FWIW: I would expect the first solution to be considerably faster.
– Stephen C
Jul 14 '10 at 14:29
2
@Stephen C: Absolutely! But as @Uri pointed out, I'm having to solve two different problems thanks to the ambiguity of the question :) Also, I rarely usematches(): for performance, I usually store thePatternin afinal static. Pays off if the same code runs frequently.
– Carl Smotricz
Jul 14 '10 at 14:31
2
@Andreas_D: Heh, I got my orders! The OP said he wanted to check a string or char array, he never said anything about nulls! :) *checks the fine print in the contract* "nullis not a string!"
– Carl Smotricz
Jul 14 '10 at 15:01
1
Also, "\w" only matches a limited subset of non-whitespace characters rather than all non-whitespace since it refers to "word characters" defined as A-Z, a-z, 0-9, and underscore.
– Rob Raisch
May 28 '11 at 15:30
1
@Rob Raisch, I must object to both of your comments. Second comment first: I explicitly gave "w" as an example checking for a particular character class, not to solve the original simpler problem. First comment: No, "\w" will not match a series of word characters preceded, surrounded or followed by other characters, e.g. whitespace. String.matches() matches the entire string, not a subset.
– Carl Smotricz
Dec 4 '11 at 11:51
|
show 3 more comments
Shortest solution I can think of:
if (string.trim().length() > 0) ...
This only checks for (non) white space. If you want to check for particular character classes, you need to use the mighty match() with a regexp such as:
if (string.matches(".*\w.*")) ...
...which checks for at least one (ASCII) alphanumeric character.
7
FWIW: I would expect the first solution to be considerably faster.
– Stephen C
Jul 14 '10 at 14:29
2
@Stephen C: Absolutely! But as @Uri pointed out, I'm having to solve two different problems thanks to the ambiguity of the question :) Also, I rarely usematches(): for performance, I usually store thePatternin afinal static. Pays off if the same code runs frequently.
– Carl Smotricz
Jul 14 '10 at 14:31
2
@Andreas_D: Heh, I got my orders! The OP said he wanted to check a string or char array, he never said anything about nulls! :) *checks the fine print in the contract* "nullis not a string!"
– Carl Smotricz
Jul 14 '10 at 15:01
1
Also, "\w" only matches a limited subset of non-whitespace characters rather than all non-whitespace since it refers to "word characters" defined as A-Z, a-z, 0-9, and underscore.
– Rob Raisch
May 28 '11 at 15:30
1
@Rob Raisch, I must object to both of your comments. Second comment first: I explicitly gave "w" as an example checking for a particular character class, not to solve the original simpler problem. First comment: No, "\w" will not match a series of word characters preceded, surrounded or followed by other characters, e.g. whitespace. String.matches() matches the entire string, not a subset.
– Carl Smotricz
Dec 4 '11 at 11:51
|
show 3 more comments
Shortest solution I can think of:
if (string.trim().length() > 0) ...
This only checks for (non) white space. If you want to check for particular character classes, you need to use the mighty match() with a regexp such as:
if (string.matches(".*\w.*")) ...
...which checks for at least one (ASCII) alphanumeric character.
Shortest solution I can think of:
if (string.trim().length() > 0) ...
This only checks for (non) white space. If you want to check for particular character classes, you need to use the mighty match() with a regexp such as:
if (string.matches(".*\w.*")) ...
...which checks for at least one (ASCII) alphanumeric character.
edited Mar 5 '16 at 17:44
answered Jul 14 '10 at 14:24
Carl SmotriczCarl Smotricz
54.2k16112156
54.2k16112156
7
FWIW: I would expect the first solution to be considerably faster.
– Stephen C
Jul 14 '10 at 14:29
2
@Stephen C: Absolutely! But as @Uri pointed out, I'm having to solve two different problems thanks to the ambiguity of the question :) Also, I rarely usematches(): for performance, I usually store thePatternin afinal static. Pays off if the same code runs frequently.
– Carl Smotricz
Jul 14 '10 at 14:31
2
@Andreas_D: Heh, I got my orders! The OP said he wanted to check a string or char array, he never said anything about nulls! :) *checks the fine print in the contract* "nullis not a string!"
– Carl Smotricz
Jul 14 '10 at 15:01
1
Also, "\w" only matches a limited subset of non-whitespace characters rather than all non-whitespace since it refers to "word characters" defined as A-Z, a-z, 0-9, and underscore.
– Rob Raisch
May 28 '11 at 15:30
1
@Rob Raisch, I must object to both of your comments. Second comment first: I explicitly gave "w" as an example checking for a particular character class, not to solve the original simpler problem. First comment: No, "\w" will not match a series of word characters preceded, surrounded or followed by other characters, e.g. whitespace. String.matches() matches the entire string, not a subset.
– Carl Smotricz
Dec 4 '11 at 11:51
|
show 3 more comments
7
FWIW: I would expect the first solution to be considerably faster.
– Stephen C
Jul 14 '10 at 14:29
2
@Stephen C: Absolutely! But as @Uri pointed out, I'm having to solve two different problems thanks to the ambiguity of the question :) Also, I rarely usematches(): for performance, I usually store thePatternin afinal static. Pays off if the same code runs frequently.
– Carl Smotricz
Jul 14 '10 at 14:31
2
@Andreas_D: Heh, I got my orders! The OP said he wanted to check a string or char array, he never said anything about nulls! :) *checks the fine print in the contract* "nullis not a string!"
– Carl Smotricz
Jul 14 '10 at 15:01
1
Also, "\w" only matches a limited subset of non-whitespace characters rather than all non-whitespace since it refers to "word characters" defined as A-Z, a-z, 0-9, and underscore.
– Rob Raisch
May 28 '11 at 15:30
1
@Rob Raisch, I must object to both of your comments. Second comment first: I explicitly gave "w" as an example checking for a particular character class, not to solve the original simpler problem. First comment: No, "\w" will not match a series of word characters preceded, surrounded or followed by other characters, e.g. whitespace. String.matches() matches the entire string, not a subset.
– Carl Smotricz
Dec 4 '11 at 11:51
7
7
FWIW: I would expect the first solution to be considerably faster.
– Stephen C
Jul 14 '10 at 14:29
FWIW: I would expect the first solution to be considerably faster.
– Stephen C
Jul 14 '10 at 14:29
2
2
@Stephen C: Absolutely! But as @Uri pointed out, I'm having to solve two different problems thanks to the ambiguity of the question :) Also, I rarely use
matches(): for performance, I usually store the Pattern in a final static. Pays off if the same code runs frequently.– Carl Smotricz
Jul 14 '10 at 14:31
@Stephen C: Absolutely! But as @Uri pointed out, I'm having to solve two different problems thanks to the ambiguity of the question :) Also, I rarely use
matches(): for performance, I usually store the Pattern in a final static. Pays off if the same code runs frequently.– Carl Smotricz
Jul 14 '10 at 14:31
2
2
@Andreas_D: Heh, I got my orders! The OP said he wanted to check a string or char array, he never said anything about nulls! :) *checks the fine print in the contract* "
null is not a string!"– Carl Smotricz
Jul 14 '10 at 15:01
@Andreas_D: Heh, I got my orders! The OP said he wanted to check a string or char array, he never said anything about nulls! :) *checks the fine print in the contract* "
null is not a string!"– Carl Smotricz
Jul 14 '10 at 15:01
1
1
Also, "\w" only matches a limited subset of non-whitespace characters rather than all non-whitespace since it refers to "word characters" defined as A-Z, a-z, 0-9, and underscore.
– Rob Raisch
May 28 '11 at 15:30
Also, "\w" only matches a limited subset of non-whitespace characters rather than all non-whitespace since it refers to "word characters" defined as A-Z, a-z, 0-9, and underscore.
– Rob Raisch
May 28 '11 at 15:30
1
1
@Rob Raisch, I must object to both of your comments. Second comment first: I explicitly gave "w" as an example checking for a particular character class, not to solve the original simpler problem. First comment: No, "\w" will not match a series of word characters preceded, surrounded or followed by other characters, e.g. whitespace. String.matches() matches the entire string, not a subset.
– Carl Smotricz
Dec 4 '11 at 11:51
@Rob Raisch, I must object to both of your comments. Second comment first: I explicitly gave "w" as an example checking for a particular character class, not to solve the original simpler problem. First comment: No, "\w" will not match a series of word characters preceded, surrounded or followed by other characters, e.g. whitespace. String.matches() matches the entire string, not a subset.
– Carl Smotricz
Dec 4 '11 at 11:51
|
show 3 more comments
I would use the Apache Commons Lang library. It has a class called StringUtils that is useful for all sorts of String operations. For checking if a String is not all whitespaces, you can use the following:
StringUtils.isBlank(<your string>)
Here is the reference: StringUtils.isBlank
8
I prefer this solution compared to using the chosen answer. This will also check for string == null
– Richard
Jul 14 '10 at 16:57
This is now incorrect.StringUtils.isEmptywill now return false if you pass in " ".
– James Spence
May 30 '17 at 20:25
add a comment |
I would use the Apache Commons Lang library. It has a class called StringUtils that is useful for all sorts of String operations. For checking if a String is not all whitespaces, you can use the following:
StringUtils.isBlank(<your string>)
Here is the reference: StringUtils.isBlank
8
I prefer this solution compared to using the chosen answer. This will also check for string == null
– Richard
Jul 14 '10 at 16:57
This is now incorrect.StringUtils.isEmptywill now return false if you pass in " ".
– James Spence
May 30 '17 at 20:25
add a comment |
I would use the Apache Commons Lang library. It has a class called StringUtils that is useful for all sorts of String operations. For checking if a String is not all whitespaces, you can use the following:
StringUtils.isBlank(<your string>)
Here is the reference: StringUtils.isBlank
I would use the Apache Commons Lang library. It has a class called StringUtils that is useful for all sorts of String operations. For checking if a String is not all whitespaces, you can use the following:
StringUtils.isBlank(<your string>)
Here is the reference: StringUtils.isBlank
edited Jan 27 '18 at 6:38
Pang
6,9011664101
6,9011664101
answered Jul 14 '10 at 14:28
Chris JChris J
6,54573439
6,54573439
8
I prefer this solution compared to using the chosen answer. This will also check for string == null
– Richard
Jul 14 '10 at 16:57
This is now incorrect.StringUtils.isEmptywill now return false if you pass in " ".
– James Spence
May 30 '17 at 20:25
add a comment |
8
I prefer this solution compared to using the chosen answer. This will also check for string == null
– Richard
Jul 14 '10 at 16:57
This is now incorrect.StringUtils.isEmptywill now return false if you pass in " ".
– James Spence
May 30 '17 at 20:25
8
8
I prefer this solution compared to using the chosen answer. This will also check for string == null
– Richard
Jul 14 '10 at 16:57
I prefer this solution compared to using the chosen answer. This will also check for string == null
– Richard
Jul 14 '10 at 16:57
This is now incorrect.
StringUtils.isEmpty will now return false if you pass in " ".– James Spence
May 30 '17 at 20:25
This is now incorrect.
StringUtils.isEmpty will now return false if you pass in " ".– James Spence
May 30 '17 at 20:25
add a comment |
Slightly shorter than what was mentioned by Carl Smotricz:
!string.trim().isEmpty();
7
You young whippersnappers and your newfangled post-Java-1.6 trickery! Seriously, at least one project in my company still runs on Java 1.4 (sigh).
– Carl Smotricz
Dec 4 '11 at 11:54
Shorter? Yes. Personally, I like the more verbose coding style
– Michel
Mar 12 '14 at 11:34
add a comment |
Slightly shorter than what was mentioned by Carl Smotricz:
!string.trim().isEmpty();
7
You young whippersnappers and your newfangled post-Java-1.6 trickery! Seriously, at least one project in my company still runs on Java 1.4 (sigh).
– Carl Smotricz
Dec 4 '11 at 11:54
Shorter? Yes. Personally, I like the more verbose coding style
– Michel
Mar 12 '14 at 11:34
add a comment |
Slightly shorter than what was mentioned by Carl Smotricz:
!string.trim().isEmpty();
Slightly shorter than what was mentioned by Carl Smotricz:
!string.trim().isEmpty();
answered Apr 7 '11 at 16:20
redslimeredslime
39133
39133
7
You young whippersnappers and your newfangled post-Java-1.6 trickery! Seriously, at least one project in my company still runs on Java 1.4 (sigh).
– Carl Smotricz
Dec 4 '11 at 11:54
Shorter? Yes. Personally, I like the more verbose coding style
– Michel
Mar 12 '14 at 11:34
add a comment |
7
You young whippersnappers and your newfangled post-Java-1.6 trickery! Seriously, at least one project in my company still runs on Java 1.4 (sigh).
– Carl Smotricz
Dec 4 '11 at 11:54
Shorter? Yes. Personally, I like the more verbose coding style
– Michel
Mar 12 '14 at 11:34
7
7
You young whippersnappers and your newfangled post-Java-1.6 trickery! Seriously, at least one project in my company still runs on Java 1.4 (sigh).
– Carl Smotricz
Dec 4 '11 at 11:54
You young whippersnappers and your newfangled post-Java-1.6 trickery! Seriously, at least one project in my company still runs on Java 1.4 (sigh).
– Carl Smotricz
Dec 4 '11 at 11:54
Shorter? Yes. Personally, I like the more verbose coding style
– Michel
Mar 12 '14 at 11:34
Shorter? Yes. Personally, I like the more verbose coding style
– Michel
Mar 12 '14 at 11:34
add a comment |
StringUtils.isBlank(CharSequence)
https://commons.apache.org/proper/commons-lang/javadocs/api-release/org/apache/commons/lang3/StringUtils.html#isBlank-java.lang.CharSequence-
Hmm, my compiler doesn't recognize this method in StringUtils.
– Scott Biggs
Jan 17 at 21:22
add a comment |
StringUtils.isBlank(CharSequence)
https://commons.apache.org/proper/commons-lang/javadocs/api-release/org/apache/commons/lang3/StringUtils.html#isBlank-java.lang.CharSequence-
Hmm, my compiler doesn't recognize this method in StringUtils.
– Scott Biggs
Jan 17 at 21:22
add a comment |
StringUtils.isBlank(CharSequence)
https://commons.apache.org/proper/commons-lang/javadocs/api-release/org/apache/commons/lang3/StringUtils.html#isBlank-java.lang.CharSequence-
StringUtils.isBlank(CharSequence)
https://commons.apache.org/proper/commons-lang/javadocs/api-release/org/apache/commons/lang3/StringUtils.html#isBlank-java.lang.CharSequence-
edited Jan 27 '18 at 6:34
Pang
6,9011664101
6,9011664101
answered Jul 14 '10 at 14:26
darelfdarelf
2,15611210
2,15611210
Hmm, my compiler doesn't recognize this method in StringUtils.
– Scott Biggs
Jan 17 at 21:22
add a comment |
Hmm, my compiler doesn't recognize this method in StringUtils.
– Scott Biggs
Jan 17 at 21:22
Hmm, my compiler doesn't recognize this method in StringUtils.
– Scott Biggs
Jan 17 at 21:22
Hmm, my compiler doesn't recognize this method in StringUtils.
– Scott Biggs
Jan 17 at 21:22
add a comment |
if(target.matches("\S"))
// then string contains at least one non-whitespace character
Note use of back-slash cap-S, meaning "non-whitespace char"
I'd wager this is the simplest (and perhaps the fastest?) solution.
1
Let try:String year="1995"; year.matches("\S"); will return falseSo this is not correct solution. :|
– Nhat Dinh
May 20 '15 at 2:59
6
Nhat, you are correct though I'm at a loss to explain why. According to the Java docs, String.matches checks to see if a given string matches a regex. A little experimentation shows that this is not entirely accurate, as this function appears to match ONLY if the provided regex matches the ENTIRE string! So, changing the regex above ("\S") to "^.*\S.*$" will work as expected, though this behavior isn't correctly documented and appears to diverge substantially from every other implementation of string matching using Regular Expressions.
– Rob Raisch
May 26 '15 at 15:46
add a comment |
if(target.matches("\S"))
// then string contains at least one non-whitespace character
Note use of back-slash cap-S, meaning "non-whitespace char"
I'd wager this is the simplest (and perhaps the fastest?) solution.
1
Let try:String year="1995"; year.matches("\S"); will return falseSo this is not correct solution. :|
– Nhat Dinh
May 20 '15 at 2:59
6
Nhat, you are correct though I'm at a loss to explain why. According to the Java docs, String.matches checks to see if a given string matches a regex. A little experimentation shows that this is not entirely accurate, as this function appears to match ONLY if the provided regex matches the ENTIRE string! So, changing the regex above ("\S") to "^.*\S.*$" will work as expected, though this behavior isn't correctly documented and appears to diverge substantially from every other implementation of string matching using Regular Expressions.
– Rob Raisch
May 26 '15 at 15:46
add a comment |
if(target.matches("\S"))
// then string contains at least one non-whitespace character
Note use of back-slash cap-S, meaning "non-whitespace char"
I'd wager this is the simplest (and perhaps the fastest?) solution.
if(target.matches("\S"))
// then string contains at least one non-whitespace character
Note use of back-slash cap-S, meaning "non-whitespace char"
I'd wager this is the simplest (and perhaps the fastest?) solution.
answered May 27 '11 at 22:08
Rob RaischRob Raisch
12.4k13345
12.4k13345
1
Let try:String year="1995"; year.matches("\S"); will return falseSo this is not correct solution. :|
– Nhat Dinh
May 20 '15 at 2:59
6
Nhat, you are correct though I'm at a loss to explain why. According to the Java docs, String.matches checks to see if a given string matches a regex. A little experimentation shows that this is not entirely accurate, as this function appears to match ONLY if the provided regex matches the ENTIRE string! So, changing the regex above ("\S") to "^.*\S.*$" will work as expected, though this behavior isn't correctly documented and appears to diverge substantially from every other implementation of string matching using Regular Expressions.
– Rob Raisch
May 26 '15 at 15:46
add a comment |
1
Let try:String year="1995"; year.matches("\S"); will return falseSo this is not correct solution. :|
– Nhat Dinh
May 20 '15 at 2:59
6
Nhat, you are correct though I'm at a loss to explain why. According to the Java docs, String.matches checks to see if a given string matches a regex. A little experimentation shows that this is not entirely accurate, as this function appears to match ONLY if the provided regex matches the ENTIRE string! So, changing the regex above ("\S") to "^.*\S.*$" will work as expected, though this behavior isn't correctly documented and appears to diverge substantially from every other implementation of string matching using Regular Expressions.
– Rob Raisch
May 26 '15 at 15:46
1
1
Let try:
String year="1995"; year.matches("\S"); will return false So this is not correct solution. :|– Nhat Dinh
May 20 '15 at 2:59
Let try:
String year="1995"; year.matches("\S"); will return false So this is not correct solution. :|– Nhat Dinh
May 20 '15 at 2:59
6
6
Nhat, you are correct though I'm at a loss to explain why. According to the Java docs, String.matches checks to see if a given string matches a regex. A little experimentation shows that this is not entirely accurate, as this function appears to match ONLY if the provided regex matches the ENTIRE string! So, changing the regex above ("\S") to "^.*\S.*$" will work as expected, though this behavior isn't correctly documented and appears to diverge substantially from every other implementation of string matching using Regular Expressions.
– Rob Raisch
May 26 '15 at 15:46
Nhat, you are correct though I'm at a loss to explain why. According to the Java docs, String.matches checks to see if a given string matches a regex. A little experimentation shows that this is not entirely accurate, as this function appears to match ONLY if the provided regex matches the ENTIRE string! So, changing the regex above ("\S") to "^.*\S.*$" will work as expected, though this behavior isn't correctly documented and appears to diverge substantially from every other implementation of string matching using Regular Expressions.
– Rob Raisch
May 26 '15 at 15:46
add a comment |
This answer focusses more on the sidenote "i.e. has at least one alphanumeric character". Besides that, it doesn't add too much to the other (earlier) solution, except that it doesn't hurt you with NPE in case the String is null.
We want false if (1) s is null or (2) s is empty or (3) s only contains whitechars.
public static boolean containsNonWhitespaceChar(String s) "".equals(s.trim()));
add a comment |
This answer focusses more on the sidenote "i.e. has at least one alphanumeric character". Besides that, it doesn't add too much to the other (earlier) solution, except that it doesn't hurt you with NPE in case the String is null.
We want false if (1) s is null or (2) s is empty or (3) s only contains whitechars.
public static boolean containsNonWhitespaceChar(String s) "".equals(s.trim()));
add a comment |
This answer focusses more on the sidenote "i.e. has at least one alphanumeric character". Besides that, it doesn't add too much to the other (earlier) solution, except that it doesn't hurt you with NPE in case the String is null.
We want false if (1) s is null or (2) s is empty or (3) s only contains whitechars.
public static boolean containsNonWhitespaceChar(String s) "".equals(s.trim()));
This answer focusses more on the sidenote "i.e. has at least one alphanumeric character". Besides that, it doesn't add too much to the other (earlier) solution, except that it doesn't hurt you with NPE in case the String is null.
We want false if (1) s is null or (2) s is empty or (3) s only contains whitechars.
public static boolean containsNonWhitespaceChar(String s) "".equals(s.trim()));
edited Jul 14 '10 at 16:38
answered Jul 14 '10 at 14:49
Andreas_DAndreas_D
95.3k11144231
95.3k11144231
add a comment |
add a comment |
If you are only checking for whitespace and don't care about null then you can use org.apache.commons.lang.StringUtils.isWhitespace(String str),
StringUtils.isWhitespace(String str);
(Checks if the String contains only whitespace.)
If you also want to check for null(including whitespace) then
StringUtils.isBlank(String str);
add a comment |
If you are only checking for whitespace and don't care about null then you can use org.apache.commons.lang.StringUtils.isWhitespace(String str),
StringUtils.isWhitespace(String str);
(Checks if the String contains only whitespace.)
If you also want to check for null(including whitespace) then
StringUtils.isBlank(String str);
add a comment |
If you are only checking for whitespace and don't care about null then you can use org.apache.commons.lang.StringUtils.isWhitespace(String str),
StringUtils.isWhitespace(String str);
(Checks if the String contains only whitespace.)
If you also want to check for null(including whitespace) then
StringUtils.isBlank(String str);
If you are only checking for whitespace and don't care about null then you can use org.apache.commons.lang.StringUtils.isWhitespace(String str),
StringUtils.isWhitespace(String str);
(Checks if the String contains only whitespace.)
If you also want to check for null(including whitespace) then
StringUtils.isBlank(String str);
edited Sep 9 '16 at 20:40
answered Sep 8 '16 at 21:06
ArunArun
1,03711226
1,03711226
add a comment |
add a comment |
With JDK/11, now you can make use of the String.isBlank API to check if the given string is not all made up of
String str1 = " ";
System.out.println(str1.isBlank()); // made up of all whitespaces, prints true
String str2 = " a";
System.out.println(str2.isBlank()); // prints false
The javadoc for the same is :
/**
* Returns @code true if the string is empty or contains only
* @link Character#isWhitespace(int) white space codepoints,
* otherwise @code false.
*
* @return @code true if the string is empty or contains only
* @link Character#isWhitespace(int) white space codepoints,
* otherwise @code false
*
* @since 11
*/
public boolean isBlank()
add a comment |
With JDK/11, now you can make use of the String.isBlank API to check if the given string is not all made up of
String str1 = " ";
System.out.println(str1.isBlank()); // made up of all whitespaces, prints true
String str2 = " a";
System.out.println(str2.isBlank()); // prints false
The javadoc for the same is :
/**
* Returns @code true if the string is empty or contains only
* @link Character#isWhitespace(int) white space codepoints,
* otherwise @code false.
*
* @return @code true if the string is empty or contains only
* @link Character#isWhitespace(int) white space codepoints,
* otherwise @code false
*
* @since 11
*/
public boolean isBlank()
add a comment |
With JDK/11, now you can make use of the String.isBlank API to check if the given string is not all made up of
String str1 = " ";
System.out.println(str1.isBlank()); // made up of all whitespaces, prints true
String str2 = " a";
System.out.println(str2.isBlank()); // prints false
The javadoc for the same is :
/**
* Returns @code true if the string is empty or contains only
* @link Character#isWhitespace(int) white space codepoints,
* otherwise @code false.
*
* @return @code true if the string is empty or contains only
* @link Character#isWhitespace(int) white space codepoints,
* otherwise @code false
*
* @since 11
*/
public boolean isBlank()
With JDK/11, now you can make use of the String.isBlank API to check if the given string is not all made up of
String str1 = " ";
System.out.println(str1.isBlank()); // made up of all whitespaces, prints true
String str2 = " a";
System.out.println(str2.isBlank()); // prints false
The javadoc for the same is :
/**
* Returns @code true if the string is empty or contains only
* @link Character#isWhitespace(int) white space codepoints,
* otherwise @code false.
*
* @return @code true if the string is empty or contains only
* @link Character#isWhitespace(int) white space codepoints,
* otherwise @code false
*
* @since 11
*/
public boolean isBlank()
edited Jun 8 '18 at 3:27
answered May 31 '18 at 19:00
nullpointernullpointer
46.5k1199190
46.5k1199190
add a comment |
add a comment |
The trim method should work great for you.
http://download.oracle.com/docs/cd/E17476_01/javase/1.4.2/docs/api/java/lang/String.html#trim()
Returns a copy of the string, with
leading and trailing whitespace
omitted. If this String object
represents an empty character
sequence, or the first and last
characters of character sequence
represented by this String object both
have codes greater than 'u0020' (the
space character), then a reference to
this String object is returned.
Otherwise, if there is no character
with a code greater than 'u0020' in
the string, then a new String object
representing an empty string is
created and returned.
Otherwise, let k be the index of the
first character in the string whose
code is greater than 'u0020', and let
m be the index of the last character
in the string whose code is greater
than 'u0020'. A new String object is
created, representing the substring of
this string that begins with the
character at index k and ends with the
character at index m-that is, the
result of this.substring(k, m+1).
This method may be used to trim
whitespace from the beginning and end
of a string; in fact, it trims all
ASCII control characters as well.
Returns: A copy of this string with
leading and trailing white space
removed, or this string if it has no
leading or trailing white space.leading or trailing white space.
You could trim and then compare to an empty string or possibly check the length for 0.
Link in answer is dead - 404 | We're sorry, the page does not exist or is no longer available.
– Pang
Jan 27 '18 at 6:35
add a comment |
The trim method should work great for you.
http://download.oracle.com/docs/cd/E17476_01/javase/1.4.2/docs/api/java/lang/String.html#trim()
Returns a copy of the string, with
leading and trailing whitespace
omitted. If this String object
represents an empty character
sequence, or the first and last
characters of character sequence
represented by this String object both
have codes greater than 'u0020' (the
space character), then a reference to
this String object is returned.
Otherwise, if there is no character
with a code greater than 'u0020' in
the string, then a new String object
representing an empty string is
created and returned.
Otherwise, let k be the index of the
first character in the string whose
code is greater than 'u0020', and let
m be the index of the last character
in the string whose code is greater
than 'u0020'. A new String object is
created, representing the substring of
this string that begins with the
character at index k and ends with the
character at index m-that is, the
result of this.substring(k, m+1).
This method may be used to trim
whitespace from the beginning and end
of a string; in fact, it trims all
ASCII control characters as well.
Returns: A copy of this string with
leading and trailing white space
removed, or this string if it has no
leading or trailing white space.leading or trailing white space.
You could trim and then compare to an empty string or possibly check the length for 0.
Link in answer is dead - 404 | We're sorry, the page does not exist or is no longer available.
– Pang
Jan 27 '18 at 6:35
add a comment |
The trim method should work great for you.
http://download.oracle.com/docs/cd/E17476_01/javase/1.4.2/docs/api/java/lang/String.html#trim()
Returns a copy of the string, with
leading and trailing whitespace
omitted. If this String object
represents an empty character
sequence, or the first and last
characters of character sequence
represented by this String object both
have codes greater than 'u0020' (the
space character), then a reference to
this String object is returned.
Otherwise, if there is no character
with a code greater than 'u0020' in
the string, then a new String object
representing an empty string is
created and returned.
Otherwise, let k be the index of the
first character in the string whose
code is greater than 'u0020', and let
m be the index of the last character
in the string whose code is greater
than 'u0020'. A new String object is
created, representing the substring of
this string that begins with the
character at index k and ends with the
character at index m-that is, the
result of this.substring(k, m+1).
This method may be used to trim
whitespace from the beginning and end
of a string; in fact, it trims all
ASCII control characters as well.
Returns: A copy of this string with
leading and trailing white space
removed, or this string if it has no
leading or trailing white space.leading or trailing white space.
You could trim and then compare to an empty string or possibly check the length for 0.
The trim method should work great for you.
http://download.oracle.com/docs/cd/E17476_01/javase/1.4.2/docs/api/java/lang/String.html#trim()
Returns a copy of the string, with
leading and trailing whitespace
omitted. If this String object
represents an empty character
sequence, or the first and last
characters of character sequence
represented by this String object both
have codes greater than 'u0020' (the
space character), then a reference to
this String object is returned.
Otherwise, if there is no character
with a code greater than 'u0020' in
the string, then a new String object
representing an empty string is
created and returned.
Otherwise, let k be the index of the
first character in the string whose
code is greater than 'u0020', and let
m be the index of the last character
in the string whose code is greater
than 'u0020'. A new String object is
created, representing the substring of
this string that begins with the
character at index k and ends with the
character at index m-that is, the
result of this.substring(k, m+1).
This method may be used to trim
whitespace from the beginning and end
of a string; in fact, it trims all
ASCII control characters as well.
Returns: A copy of this string with
leading and trailing white space
removed, or this string if it has no
leading or trailing white space.leading or trailing white space.
You could trim and then compare to an empty string or possibly check the length for 0.
answered Jul 14 '10 at 14:27
Corey OgburnCorey Ogburn
11.7k1986152
11.7k1986152
Link in answer is dead - 404 | We're sorry, the page does not exist or is no longer available.
– Pang
Jan 27 '18 at 6:35
add a comment |
Link in answer is dead - 404 | We're sorry, the page does not exist or is no longer available.
– Pang
Jan 27 '18 at 6:35
Link in answer is dead - 404 | We're sorry, the page does not exist or is no longer available.
– Pang
Jan 27 '18 at 6:35
Link in answer is dead - 404 | We're sorry, the page does not exist or is no longer available.
– Pang
Jan 27 '18 at 6:35
add a comment |
If you are using Java 11, the new isBlank string method will come in handy:
!s.isBlank();
If you are using Java 8, 9 or 10, you could build a simple stream to check that a string is not whitespace only:
!s.chars().allMatch(Character::isWhitespace));
In addition to not requiring any third-party libraries such as Apache Commons Lang, these solutions have the advantage of handling any white space character, and not just plain ' ' spaces as would a trim-based solution suggested in many other answers. You can refer to the Javadocs for an exhaustive list of all supported white space types. Note that empty strings are also covered in both cases.
add a comment |
If you are using Java 11, the new isBlank string method will come in handy:
!s.isBlank();
If you are using Java 8, 9 or 10, you could build a simple stream to check that a string is not whitespace only:
!s.chars().allMatch(Character::isWhitespace));
In addition to not requiring any third-party libraries such as Apache Commons Lang, these solutions have the advantage of handling any white space character, and not just plain ' ' spaces as would a trim-based solution suggested in many other answers. You can refer to the Javadocs for an exhaustive list of all supported white space types. Note that empty strings are also covered in both cases.
add a comment |
If you are using Java 11, the new isBlank string method will come in handy:
!s.isBlank();
If you are using Java 8, 9 or 10, you could build a simple stream to check that a string is not whitespace only:
!s.chars().allMatch(Character::isWhitespace));
In addition to not requiring any third-party libraries such as Apache Commons Lang, these solutions have the advantage of handling any white space character, and not just plain ' ' spaces as would a trim-based solution suggested in many other answers. You can refer to the Javadocs for an exhaustive list of all supported white space types. Note that empty strings are also covered in both cases.
If you are using Java 11, the new isBlank string method will come in handy:
!s.isBlank();
If you are using Java 8, 9 or 10, you could build a simple stream to check that a string is not whitespace only:
!s.chars().allMatch(Character::isWhitespace));
In addition to not requiring any third-party libraries such as Apache Commons Lang, these solutions have the advantage of handling any white space character, and not just plain ' ' spaces as would a trim-based solution suggested in many other answers. You can refer to the Javadocs for an exhaustive list of all supported white space types. Note that empty strings are also covered in both cases.
edited Sep 26 '18 at 17:39
answered May 5 '18 at 10:25
PyvesPyves
2,84552435
2,84552435
add a comment |
add a comment |
Alternative:
boolean isWhiteSpaces( String s )
return s != null && s.matches("\s+");
1
\s* will match all strings with or without whitespaces. Perhaps you mean \s+ ?
– Rob Raisch
May 27 '11 at 22:11
add a comment |
Alternative:
boolean isWhiteSpaces( String s )
return s != null && s.matches("\s+");
1
\s* will match all strings with or without whitespaces. Perhaps you mean \s+ ?
– Rob Raisch
May 27 '11 at 22:11
add a comment |
Alternative:
boolean isWhiteSpaces( String s )
return s != null && s.matches("\s+");
Alternative:
boolean isWhiteSpaces( String s )
return s != null && s.matches("\s+");
edited May 28 '11 at 6:14
answered Jul 14 '10 at 16:50
OscarRyzOscarRyz
142k96338513
142k96338513
1
\s* will match all strings with or without whitespaces. Perhaps you mean \s+ ?
– Rob Raisch
May 27 '11 at 22:11
add a comment |
1
\s* will match all strings with or without whitespaces. Perhaps you mean \s+ ?
– Rob Raisch
May 27 '11 at 22:11
1
1
\s* will match all strings with or without whitespaces. Perhaps you mean \s+ ?
– Rob Raisch
May 27 '11 at 22:11
\s* will match all strings with or without whitespaces. Perhaps you mean \s+ ?
– Rob Raisch
May 27 '11 at 22:11
add a comment |
trim() and other mentioned regular expression do not work for all types of whitespaces
i.e: Unicode Character 'LINE SEPARATOR' http://www.fileformat.info/info/unicode/char/2028/index.htm
Java functions Character.isWhitespace() covers all situations.
That is why already mentioned solution
StringUtils.isWhitespace( String ) /or StringUtils.isBlank(String)
should be used.
add a comment |
trim() and other mentioned regular expression do not work for all types of whitespaces
i.e: Unicode Character 'LINE SEPARATOR' http://www.fileformat.info/info/unicode/char/2028/index.htm
Java functions Character.isWhitespace() covers all situations.
That is why already mentioned solution
StringUtils.isWhitespace( String ) /or StringUtils.isBlank(String)
should be used.
add a comment |
trim() and other mentioned regular expression do not work for all types of whitespaces
i.e: Unicode Character 'LINE SEPARATOR' http://www.fileformat.info/info/unicode/char/2028/index.htm
Java functions Character.isWhitespace() covers all situations.
That is why already mentioned solution
StringUtils.isWhitespace( String ) /or StringUtils.isBlank(String)
should be used.
trim() and other mentioned regular expression do not work for all types of whitespaces
i.e: Unicode Character 'LINE SEPARATOR' http://www.fileformat.info/info/unicode/char/2028/index.htm
Java functions Character.isWhitespace() covers all situations.
That is why already mentioned solution
StringUtils.isWhitespace( String ) /or StringUtils.isBlank(String)
should be used.
edited Oct 3 '17 at 11:46
answered Oct 3 '17 at 11:39
andreyroandreyro
493715
493715
add a comment |
add a comment |
StringUtils.isEmptyOrWhitespaceOnly(<your string>)
will check :
- is it null
- is it only space
- is it empty string ""
https://www.programcreek.com/java-api-examples/?class=com.mysql.jdbc.StringUtils&method=isEmptyOrWhitespaceOnly
Is this from a library? Link to the project. Or built into Java? Indicate the package.
– Basil Bourque
Dec 24 '18 at 7:59
@BasilBourque i think this is com.mysql.jdbc.StringUtils.isEmptyOrWhitespaceOnly
– ahmet_y
Dec 24 '18 at 9:31
add a comment |
StringUtils.isEmptyOrWhitespaceOnly(<your string>)
will check :
- is it null
- is it only space
- is it empty string ""
https://www.programcreek.com/java-api-examples/?class=com.mysql.jdbc.StringUtils&method=isEmptyOrWhitespaceOnly
Is this from a library? Link to the project. Or built into Java? Indicate the package.
– Basil Bourque
Dec 24 '18 at 7:59
@BasilBourque i think this is com.mysql.jdbc.StringUtils.isEmptyOrWhitespaceOnly
– ahmet_y
Dec 24 '18 at 9:31
add a comment |
StringUtils.isEmptyOrWhitespaceOnly(<your string>)
will check :
- is it null
- is it only space
- is it empty string ""
https://www.programcreek.com/java-api-examples/?class=com.mysql.jdbc.StringUtils&method=isEmptyOrWhitespaceOnly
StringUtils.isEmptyOrWhitespaceOnly(<your string>)
will check :
- is it null
- is it only space
- is it empty string ""
https://www.programcreek.com/java-api-examples/?class=com.mysql.jdbc.StringUtils&method=isEmptyOrWhitespaceOnly
edited Dec 24 '18 at 9:37
answered Dec 24 '18 at 6:56
akakargulakakargul
367
367
Is this from a library? Link to the project. Or built into Java? Indicate the package.
– Basil Bourque
Dec 24 '18 at 7:59
@BasilBourque i think this is com.mysql.jdbc.StringUtils.isEmptyOrWhitespaceOnly
– ahmet_y
Dec 24 '18 at 9:31
add a comment |
Is this from a library? Link to the project. Or built into Java? Indicate the package.
– Basil Bourque
Dec 24 '18 at 7:59
@BasilBourque i think this is com.mysql.jdbc.StringUtils.isEmptyOrWhitespaceOnly
– ahmet_y
Dec 24 '18 at 9:31
Is this from a library? Link to the project. Or built into Java? Indicate the package.
– Basil Bourque
Dec 24 '18 at 7:59
Is this from a library? Link to the project. Or built into Java? Indicate the package.
– Basil Bourque
Dec 24 '18 at 7:59
@BasilBourque i think this is com.mysql.jdbc.StringUtils.isEmptyOrWhitespaceOnly
– ahmet_y
Dec 24 '18 at 9:31
@BasilBourque i think this is com.mysql.jdbc.StringUtils.isEmptyOrWhitespaceOnly
– ahmet_y
Dec 24 '18 at 9:31
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3247067%2fhow-do-i-check-that-a-java-string-is-not-all-whitespaces%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
Be aware that there are many different definitions of whitespace: spreadsheets.google.com/pub?key=pd8dAQyHbdewRsnE5x5GzKQ Which do you want? Or then you say "has an alphanumeric character", which is a completely different thing. Please clarify.
– Kevin Bourrillion
Jul 14 '10 at 15:20
Apologies for the confusion ... not all whitespaces is the key- basically if it has all whitespace characters I want to exclude it, because it has no content.
– Ankur
Jul 14 '10 at 15:27
With JDK/11 you can make use of the
String.isBlankAPI for the same.– nullpointer
May 31 '18 at 19:01