zoukankan      html  css  js  c++  java
  • 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;  
    }

    使用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的方法

  • 相关阅读:
    #转 并查集详解
    美素数
    Wireless Network
    寒假CF- WA了不要怕!
    寒假CF1 小呀小苹果儿
    (专题赛)A Bug's Life
    (周三赛)The Hardest Problem Ever
    (周三赛)A==B?
    (周三赛)还是畅通工程
    cursor改变鼠标样式
  • 原文地址:https://www.cnblogs.com/llzzy/p/3255704.html
Copyright © 2011-2022 走看看