java清除字符串前后的空格和特定字符方法

       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; 

你可能感兴趣的:(java清除字符串前后的空格和特定字符方法)