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

来源:百度知道 编辑:UC知道 时间:2024/06/08 10:26:54
给你一段字符串,将其中每2个字符间隔当中的空格都只保留一个

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

replaceAll(" +"," ");

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);
}
}
}

public class Test {

public static void main(String[] args) {

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

}

不懂什么意思,举个例子