public static String trim(String source, char c){
        String beTrim = String.valueOf(c);

        source = source.trim(); // 循环去掉字符串首的beTrim字符 
        String beginChar = source.substring(0, 1);  
        while (beginChar.equalsIgnoreCase(beTrim)) {  
            source = source.substring(1, source.length());  
            beginChar = source.substring(0, 1);  
        }  
 
       // 循环去掉字符串尾的beTrim字符  
       String endChar = source.substring(source.length() - 1, source.length());  
       while (endChar.equalsIgnoreCase(beTrim)) {  
            source = source.substring(0, source.length() - 1);  
            endChar = source.substring(source.length() - 1, source.length());  
       }  
       return source;  
}

使用trim的方法

public static String deleteBlank(String str){
  char[] array = str.toCharArray();
  int start = 0,end = array.length-1;
  while(array[start]==' ')start++;
  while(array[end]==' ')end--;
  return new String(array,start,end-start);
 }

不使用trim的方法