String Str = " zhangmin am i ,hi";
String result = "";
//方式一,完全ok
String [] array = Str.split(" +");
for(int i=array.length-1;i>=0;i--) {
String temp = array[i];
if(temp.contains(",")) {
temp = temp.replace(",", "");
temp+=",";
}
result+=temp+" ";
}
System.out.println(result.trim());
其中注意这里,
Str.split(" +")
这是按照一个或多个空格划分字符串为数组为:[, zhangmin, am, i, ,hi]