字符数组去空格不抱异常怎么解决?
String[] red = new String[]{ " 123 123123 ", " 123123123 ", " 123 ", " 321 2 "};
一个这样的字符数组怎么样把里面的空格给去掉
for(int i = 0; i < red.length; i++)
red[i] = red[i].trim();
这样做不行?
------解决方案--------------------for(int i=0;i <red.length;i++){
String temp=new StringBuilder(String red[i]);
for(int j=0;j <temp.length;j++){
if(temp.charAt(j).equals( " "))
temp.charAt(j).deleteCharAt(j);
}
}
呵呵,这个方法比较笨
------解决方案--------------------for(int i=0;i <red.length;i++){
{
red[i]= red[i].replaceAll( " ", " ");
}
------解决方案--------------------这样?
String[] red = new String[]{ " 123 123123 ", " 123123123 ", " 123 ", " 321 2 "};
int i = 0;
for(String str: red) {
red[i] = str.replaceAll( " ", " ");
++i;
}
System.out.println(Arrays.toString(red));