第1个回答 2010-04-28
不用那么麻烦,用正则表达式很简单
public static void main(String args[])
{
String str = "aa bb cc dd";
String test = str.replaceAll("\\s{1,}", " ");
System.out.println(test);
}
把所有多于1个的空格替换成1个空格
第2个回答 2010-04-28
public class TestString01 {
public static void main(String[] args) {
String str = "aa bb cc dd";
/*String s1[] = str.split(" ");
for(int i=0;i<s1.length;i++){
System.out.print(s1[i]);
}*/
String s1 = str.replaceAll("aa bb cc dd", "aabbccdd");
System.out.println(s1);
}
}
第3个回答 2010-04-28
static String begin(String s)
{
int i=0;String news="";
while(i<s.length())
{
if(s.charAt(i)!=' ')new s+=s.charAt(i);
i++
}
return news;
}