如果且仅当字符串包含不包含空格的字符



我认为这是一个简单的问题,我现在只是一头雾水。在Java中,我想知道字符串是否只包含一个字符,但允许使用空格。以下是我正在努力实现的目标。

" }" = True

"}" = True

" } }" = False

使用trim()删除前导和尾随空白。然后检查结果字符串的长度。

只需使用此正则表达式:"\s*.\s*"

String regex = "\s*.\s*";
System.out.println(" }".matches(regex));   // prints true
System.out.println("}".matches(regex));    // prints true
System.out.println(" } }".matches(regex)); // prints false

最新更新