String
中是否有任何函数可以帮助删除字符串中的所有空格?我不想使用这样的方法。
private String deleteSpace(String string) {
String newString = "";
for(int i = 0;i < string.length();i++)
if(string.charAt(i) != ' ')
newString += String.valueOf(string.charAt(i));
return newString;
}
感谢您的帮助。
最佳答案
// all occurrences with literal arguments
string = string.replace(" ","");
或
// all occurrences with regular expression arguments
string = string.replaceAll("\\s+", "");
关于java - String中是否有任何函数可以帮助删除字符串中的所有空格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17773605/