String[] red = new String[]{" 123 123123 "," 123123123 "," 123 "," 321 2"};
一个这样的字符数组怎么样把里面的空格给去掉
for(int i = 0; i < red.length; i++)
red[i] = red[i].trim();
这样做不行?

解决方案 »

  1.   

    red[i] = red[i].trim();
    ===============================
    只能取掉你的左右两端的空格
    还是用replace(" ","");或者是正则replace("去空格的正则","");
    具体的正则给忘了
      

  2.   

    red[i] = red[i].trim();
    =======================
    问题是左右2边的空格都去不掉
      

  3.   

    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);
                                      }
                                  }
    呵呵,这个方法比较笨
      

  4.   

    for(int i=0;i<red.length;i++){
    {
     red[i]= red[i].replaceAll(" ","");
    }
      

  5.   

    这样?
            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));