我有一个包含单词的字符串,这些单词由一个或多个空白字符(空格,制表符等)分隔。我正在尝试编写最优化的过程,该过程以相同的顺序以相同的单词输出字符串,但仅用一个空格隔开。
我正在尝试这样做,但是仍然有问题:
public class Test {
public static void main(String args[]) {
String str = "word1, word2 word3@+word4?.word5.word6";
Stream<String> stream = Arrays.stream(input.split( "[, ?.@]+_" ));
.stream().collect(Collectors.joining(" "));
}
}
按一个或多个非单词字符拆分\W+
并使用Collectors.joining
String input = "word1, word2 word3@word4?word5.word6";
String str = Arrays.stream(input.split( "\\W+" ))
.collect(Collectors.joining(" "));
System.out.println(str);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句