Java regex - expression with exactly one whitespace

后端 未结 6 1984
攒了一身酷
攒了一身酷 2021-01-14 12:08

I want to match all expressions with exactly one whitespace. Currently, I\'m using [^\\\\s]*\\\\s[^\\\\s]*. That doesn\'t seem like a very good way, though.

6条回答
  •  情歌与酒
    2021-01-14 12:52

    You could also check it with indexOf:

    String s = "some text";
    int indexOf = s.indexOf(' ');
    boolean isOneWhitespace = (indexOf >= 0 && indexOf == s.lastIndexOf(' '));
    

提交回复
热议问题