JAVA 把字符串间的若干空格保留一个,

给你一段字符串,将其中每2个字符间隔当中的空格都只保留一个
2025-03-28 21:20:46
推荐回答(5个)
回答1:

string.replaceAll("\\s+"," ");

回答2:

public class Test {

public static void main(String[] args) {

String str ="A B C ";
System.out.println(str);
System.out.println(str.replaceAll(" ", " "));
}

}

回答3:

public class Demo {
public static void main(String[] args) {
String s = "asdkjhkjg";
String ss = "";
for (int i = 0; i < s.length(); i++) {
ss = s.charAt(i) + " ";
System.out.print(ss);
}
}
}

回答4:

replaceAll(" +"," ");

回答5:

不懂什么意思,举个例子