如何删除重复的空格(包括制表符、换行符、空格等)在字符串中使用Java?
发布于 2016-12-23 05:35:10
String str = " Text with multiple spaces ";
str = org.apache.commons.lang3.StringUtils.normalizeSpace(str);
// str = "Text with multiple spaces"
发布于 2010-10-18 20:12:58
试试这个--你必须使用import java.util.regex.*;
Pattern pattern = Pattern.compile("\\s+");
Matcher matcher = pattern.matcher(string);
boolean check = matcher.find();
String str = matcher.replaceAll(" ");
其中string
是您需要删除重复空格的字符串
发布于 2013-07-21 09:52:43
嗨,我发现的最快的(但不是最漂亮的)方式是
while (cleantext.indexOf(" ") != -1)
cleantext = StringUtils.replace(cleantext, " ", " ");
与正则表达式相比,它在android上的运行速度相当快
https://stackoverflow.com/questions/3958955
复制相似问题