合并字符串中的多个空格

合并字符串中的多个空格

String strResult= "";
String str="Senators   are        still in        the nation’s        Capitol.";
Pattern pattern = Pattern.compile("\\s+");
Matcher matcher = pattern.matcher(str);
strResult= matcher.replaceAll(" ");
System.out.println(strResult);

Senators are still in the nation’s Capitol.

 

posted @ 2020-12-20 11:20  西北逍遥  阅读(420)  评论(0编辑  收藏  举报