开发者

See if a string begins with whitespace in Java

I know that trim removes whitespace from the beginning and end of a string, but I wanted to check if the first character of a string is a whitespace. I'v开发者_开发问答e tried what seems about everything, but I can't seem to get it to work.

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

Thanks a lot!


if (Character.isWhitespace(str.charAt(0))) {
  // do something
}


if (Character.isWhitespace(str.charAt(0))) //...


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;
}


"string".startsWith(" ")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜