See if a string starts with spaces in Java

I know that trim removes spaces from the beginning and end of a line, but I need to check if the first character of the line is a space. I tried what seems to be about everything, but I can't get it to work.

Can someone point me in the right direction? I would appreciate it if regular expressions were not used.

Thanks a lot!

+4
source share
4 answers
if (Character.isWhitespace(str.charAt(0))) { // do something } 
+22
source
 if (Character.isWhitespace(str.charAt(0))) //... 
+4
source
 public void yourMethod(String string) { if (isLengthGreaterThanZero(string) && isFirstCharacterWhiteSpace(string)) { ... } } private boolean isFirstCharacterWhiteSpace(String string) { char firstCharacter = string.charAt(0); return Character.isWhitespace(firstCharacter); } private boolean isLengthGreaterThanZero(String string) { return string != null && string.length() > 0; } 
0
source
 "string".startsWith(" ") 
0
source

All Articles