public static String method(String str){
int count = 0;
String temp = "";
String kg = " ";
StringBuffer sb = new StringBuffer();
for(int i=0;i<str.length();i++){
temp = String.valueOf(str.charAt(i));
if(kg.equals(temp)){
if(count>1){
count=0;
sb.append(kg);
}
}else{
count++;
sb.append(temp);
}
}
return sb.toString();
};
把你要处理的字符串传进去,看返回值
追问有个缺陷,如果三个字都是分开的就不行了,例如:“中 国 人”,只能变成“中国 人”,还有个空格没有去掉,应该的结果是“中国人”才对!能解决一下吗?
追答public static String method(String str){
int count = 0;
String temp = "";
String kg = " ";
StringBuffer sb = new StringBuffer();
for (int i = 0; i < str.length(); i++) {
temp = String.valueOf(str.charAt(i));
if (kg.equals(temp)) {
if (count > 1) {
sb.append(kg);
}
count = 0;
} else {
count++;
sb.append(temp);
}
}
return sb.toString();
};