-1

I am trying to check is string empty or not

Simply I have used apache.lang.StringUtils isNotBlank method to check

String str = " ";
System.out.println(StringUtils.isNotBlank(str)); // Output == true
System.out.println(StringUtils.isNotBlank(str.trim())); // Output == true
// But both expected as false

But every time its printing true.

Some other string its working fine but for above str value above code not working as expected.

Dorukhan Arslan
  • 2,676
  • 2
  • 24
  • 42
Shiladittya Chakraborty
  • 4,270
  • 8
  • 45
  • 94
  • 1
    Your string has space. – Achintya Jha Dec 14 '17 at 10:09
  • You can try removing the whitespaces before checking - StringUtils.isNotBlank(str.trim()). – kevenlolo Dec 14 '17 at 10:13
  • 1
    check if `Character.isWhiteSpace(str.chatAt(0))` returns true or not. – Eran Dec 14 '17 at 10:15
  • 1
    Have you checked if this is perhaps a non-breaking space or a "hard space", i.e. alt-255? (Created in MS Word with ctrl-shift-space) – SurfMan Dec 14 '17 at 10:17
  • Are you sure about the fully qualified name? Don't you use the newest version, where the name should be `org.apache.commons.lang3.StringUtils`. It btw works correctly there. – Tom Dec 14 '17 at 10:21
  • 2
    Try this `System.out.println(Arrays.toString(str.getBytes()));` and show us what do you get in your console – Mustapha Belmokhtar Dec 14 '17 at 10:22
  • @мυѕτавєւмo I am getting output as [-96] – Shiladittya Chakraborty Dec 14 '17 at 10:25
  • 1
    @ShiladittyaChakraborty that answers your question, you should have got [32], in this case it is considered as a whitespace, a hidden character is in your `str` – Mustapha Belmokhtar Dec 14 '17 at 10:27
  • How to replace that character? – Shiladittya Chakraborty Dec 14 '17 at 10:29
  • 1
    @мυѕτавєւмo You may want to write you findings as an answer. `StringUtils.isNotBlank(new String(new byte[]{-96}))` actually returns `true`. – Tom Dec 14 '17 at 10:31
  • 1
    @ShiladittyaChakraborty These questions might help you: 1. [How can I replace non-printable Unicode characters in Java?](https://stackoverflow.com/questions/6198986/how-can-i-replace-non-printable-unicode-characters-in-java) - 2. [Fastest way to strip all non-printable characters from a Java String](https://stackoverflow.com/questions/7161534/fastest-way-to-strip-all-non-printable-characters-from-a-java-string) – OH GOD SPIDERS Dec 14 '17 at 10:45

2 Answers2

4

Some characters may appear to be whitespaces, but they aren't, or at least Java doesn't consider them as whitespaces. In your case, when you perform

System.out.println(Arrays.toString(str.getBytes()));

you got [-96] as output. -96 is an overflown byte value for 160 which is the no-breaking space character, but the Java Language Specification doesn't list that character as a whitespace character.
This why StringUtils.isNotBlank(str) returns true, because your String has non-whitespace characters. str.trim() removes whitespaces characters, thus it won't touch the existing no-breaking space.

You can reproduce that by manually creating a String with that character:

StringUtils.isNotBlank(new String(new byte[]{-96}));

or

StringUtils.isNotBlank(new String(new byte[]{(byte) 160}));

Both versions will yield true.

Tom
  • 16,842
  • 17
  • 45
  • 54
Mustapha Belmokhtar
  • 1,231
  • 8
  • 21
  • 1
    I took the liberty to add an explanation about the character in OPs String. Please review my changes :). – Tom Dec 14 '17 at 12:20
1

You could try checking the length of the string after removing all whitespace:

String input = " ";
if (input != null && input.replaceAll("\\s+", "").length() > 0) {
    System.out.println("Not empty.");
}
else {
    System.out.println("Empty");
}

I offer this an alternative to using the Apache library. The logic is that a non empty string is any string which is not null and which has at least one non whitespace character in it.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360