59

I am working with an existing framework where I have to set a certain attribute to blank if some conditions are satisfied. Unfortunately, the framework doesn't allow setting only whitespace to the attribute value. Specifically, it does a

!(org.apache.commons.lang.StringUtils.isBlank(value)) check on the value

Is it possible to somehow bypass this and set a value that looks blank/invisible to the eye but is not regarded as whitespace?

I am using a dash "-" right now, but I think it would be interesting to know if it's possible.

CodeBlue
  • 14,631
  • 33
  • 94
  • 132

4 Answers4

66

There's also (U+2800 BRAILLE PATTERN BLANK), which is a blank Braille block rather than a space character.

  • 15
    omg... THIS! You, my friend, are a genius! I needed a "blank character" to shift/justify a title string to the left, but my UI system silently rejects any characters to the right of the string that have the unicode `Whitespace_Property`. The non-Whitespace_Property "zero width" space characters mentioned above/below are just that -- zero-width -- so are of zero help. Nor would a dot suffice or anything that prints kludgey looking pixels. The character you mention is the only thing that worked for me. I'd upvote you 10x if I could. THANK YOU! – electromaggot Jan 18 '20 at 05:43
  • 3
    Whoops, fixing an error in what I just wrote: the Unicode whitespace character designation is actually called the `White_Space` property. Described here: http://www.unicode.org/reports/tr44/#White_Space – electromaggot Jan 18 '20 at 06:16
  • 2
    here because I wanted to create emoji artwork in a WhatsApp message and WhatsApp trims whitespace off the first line of the message… This did the trick! – George WS Dec 04 '20 at 07:39
  • 2
    This worked for me on iOS where ZERO WIDTH SPACE was still being automatically trimmed. Helpful for hacky fixes to oblique font clipping in system owned views like nav bar titles. – Nathan Hosselton May 03 '21 at 20:35
43

Try Unicode Character 'ZERO WIDTH SPACE' (U+200B). It is not a Whitespace according to WP: Whitespace#Unicode

The code of StringUtils.isBlank will not bother it:

public static boolean isBlank(String str) {
int strLen;
if (str == null || (strLen = str.length()) == 0) {
          return true;
     }
for (int i = 0; i < strLen; i++) {
     if ((Character.isWhitespace(str.charAt(i)) == false)) {
                   return false;
                }
         }
 return true;
  }
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Michael Konietzka
  • 5,419
  • 2
  • 28
  • 29
  • 8
    That's an extremely useful character to know, thank you. (Sign-up forms with a required last name, etc.) – Tobia Aug 11 '17 at 12:49
9

That Unicode Character 'ZERO WIDTH SPACE' (U+200B) Michael Konietzka shared didn't work for me, but found a different one that did:

‏‏‎ ‎

It actually identifies as combination of

U+200F : RIGHT-TO-LEFT MARK [RLM]
U+200F : RIGHT-TO-LEFT MARK [RLM]
U+200E : LEFT-TO-RIGHT MARK [LRM]
U+0020 : SPACE [SP]
U+200E : LEFT-TO-RIGHT MARK [LRM]

and it's ASCII value is 8207

‏‏‎'‏‏‎ ‎'.charCodeAt(0) // 8207

Source: http://emptycharacter.com/

Bugs Bunny
  • 2,496
  • 1
  • 26
  • 32
1

JavaScript's

String.fromCharCode(8287).repeat(30)

gave me real but invisible spaces.

http://emptycharacter.com/ great
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Jayanta
  • 135
  • 4