【问题标题】:Trim characters in Java在 Java 中修剪字符
【发布时间】:2011-01-06 11:29:57
【问题描述】:

如何在 Java 中修剪字符?
例如

String j = “\joe\jill\”.Trim(new char[] {“\”});

j 应该是

“乔\吉尔”

String j = “jack\joe\jill\”.Trim("jack");

j 应该是

"\乔\吉尔\"

【问题讨论】:

  • \\\\joe\\jill\\\\` should return? joe\\jill` 应该怎么做??
  • @Oscar 是的。就像 .net 中的修剪一样
  • 我觉得这个操作不叫剪裁...
  • 但我只是搜索 trim char 来找到这个问题,万岁。

标签: java string trim


【解决方案1】:

我的解决方案:

private static String trim(String string, String charSequence) {
        var str = string;
        str = str.replace(" ", "$SAVE_SPACE$").
                  replace(charSequence, " ").
                  trim().
                  replace(" ", charSequence).
                  replace("$SAVE_SPACE$", " ");
        return str;
    }

【讨论】:

    【解决方案2】:

    10 年前的问题,但感觉大多数答案都有些复杂,或者与所问的方式不太相符。此外,这里最受好评的答案也没有提供任何示例。这是我制作的一个简单的类:

    https://gist.github.com/Maxdw/d71afd11db2df4f1297ad3722d6392ec

    用法

    Trim.left("\joe\jill\", "\") == "joe\jill\"
    
    Trim.left("jack\joe\jill\", "jack") == "\joe\jill\"
    
    Trim.left("\\\\joe\\jill\\\\", "\") == "joe\\jill\\\\"
    

    【讨论】:

      【解决方案3】:

      Apache Commons 有一个很棒的StringUtils class (org.apache.commons.lang.StringUtils)。在StringUtils 中有一个strip(String, String) 方法可以满足您的需求。

      无论如何,我强烈推荐使用 Apache Commons,尤其是 Collections 和 Lang 库。

      【讨论】:

      • 非常好的解决方案。
      【解决方案4】:
      public static String trim(String value, char c) {
      
          if (c <= 32) return value.trim();
      
          int len = value.length();
          int st = 0;
          char[] val = value.toCharArray();    /* avoid getfield opcode */
      
          while ((st < len) && (val[st] == c)) {
              st++;
          }
          while ((st < len) && (val[len - 1] == c)) {
              len--;
          }
          return ((st > 0) || (len < value.length())) ? value.substring(st, len) : value;
      }
      

      【讨论】:

        【解决方案5】:

        CharMatcher – 谷歌番石榴

        在过去,我是第二个Colins’ Apache commons-lang answer。但是现在 Google 的 guava-libraries 发布了,CharMatcher 类将很好地满足您的需求:

        String j = CharMatcher.is('\\').trimFrom("\\joe\\jill\\"); 
        // j is now joe\jill
        

        CharMatcher 有一组非常简单而强大的 API 以及一些预定义的常量,使操作变得非常容易。例如:

        CharMatcher.is(':').countIn("a:b:c"); // returns 2
        CharMatcher.isNot(':').countIn("a:b:c"); // returns 3
        CharMatcher.inRange('a', 'b').countIn("a:b:c"); // returns 2
        CharMatcher.DIGIT.retainFrom("a12b34"); // returns "1234"
        CharMatcher.ASCII.negate().removeFrom("a®¶b"); // returns "ab";
        

        非常好的东西。

        【讨论】:

        • ? 请查看 Google Guava 中的 CharMatcher。光滑的东西。巧妙地使用谓词语法。可以很容易地指定您想到的空白、不可见和控制字符的各种定义中的哪一个。该文档链接到一个有趣的电子表格,其中列出了一些 various definitions of whitespace
        【解决方案6】:

        我会这样做。

        我认为它的效率已经达到了合理的程度。它优化了单个字符的大小写,避免为每个删除的子序列创建多个子字符串。

        请注意,将处理将空字符串传递给修剪的极端情况(其他一些答案将进入无限循环)。

        /** Trim all occurrences of the string <code>rmvval</code> from the left and right of <code>src</code>.  Note that <code>rmvval</code> constitutes an entire string which must match using <code>String.startsWith</code> and <code>String.endsWith</code>. */
        static public String trim(String src, String rmvval) {
            return trim(src,rmvval,rmvval,true);
            }
        
        /** Trim all occurrences of the string <code>lftval</code> from the left and <code>rgtval</code> from the right of <code>src</code>.  Note that the values to remove constitute strings which must match using <code>String.startsWith</code> and <code>String.endsWith</code>. */
        static public String trim(String src, String lftval, String rgtval, boolean igncas) {
            int                                 str=0,end=src.length();
        
            if(lftval.length()==1) {                                                    // optimize for common use - trimming a single character from left
                char chr=lftval.charAt(0);
                while(str<end && src.charAt(str)==chr) { str++; }
                }
            else if(lftval.length()>1) {                                                // handle repeated removal of a specific character sequence from left
                int vallen=lftval.length(),newstr;
                while((newstr=(str+vallen))<=end && src.regionMatches(igncas,str,lftval,0,vallen)) { str=newstr; }
                }
        
            if(rgtval.length()==1) {                                                    // optimize for common use - trimming a single character from right
                char chr=rgtval.charAt(0);
                while(str<end && src.charAt(end-1)==chr) { end--; }
                }
            else if(rgtval.length()>1) {                                                // handle repeated removal of a specific character sequence from right
                int vallen=rgtval.length(),newend;
                while(str<=(newend=(end-vallen)) && src.regionMatches(igncas,newend,rgtval,0,vallen)) { end=newend; }
                }
        
            if(str!=0 || end!=src.length()) {
                if(str<end) { src=src.substring(str,end); }                            // str is inclusive, end is exclusive
                else        { src="";                     }
                }
        
            return src;
            }
        

        【讨论】:

          【解决方案7】:

          这是另一个非正则表达式、非超级棒、非超级优化但非常容易理解的非外部库解决方案:

          public static String trimStringByString(String text, String trimBy) {
              int beginIndex = 0;
              int endIndex = text.length();
          
              while (text.substring(beginIndex, endIndex).startsWith(trimBy)) {
                  beginIndex += trimBy.length();
              } 
          
              while (text.substring(beginIndex, endIndex).endsWith(trimBy)) {
                  endIndex -= trimBy.length();
              }
          
              return text.substring(beginIndex, endIndex);
          }
          

          用法:

          String trimmedString = trimStringByString(stringToTrim, "/");
          

          【讨论】:

            【解决方案8】:

            我认为没有任何内置函数可以根据传入的字符串进行修剪。这是一个如何做到这一点的小例子。这可能不是最有效的解决方案,但对于大多数情况,它可能足够快,评估并适应您的需求。我建议对将定期使用的任何代码 sn-p 进行性能测试和优化。下面,我以一些时间信息为例。

            public String trim( String stringToTrim, String stringToRemove )
            {
                String answer = stringToTrim;
            
                while( answer.startsWith( stringToRemove ) )
                {
                    answer = answer.substring( stringToRemove.length() );
                }
            
                while( answer.endsWith( stringToRemove ) )
                {
                    answer = answer.substring( 0, answer.length() - stringToRemove.length() );
                }
            
                return answer;
            }
            

            此答案假定要修剪的字符是字符串。例如,传入“abc”会删除“abc”,但不会删除“bbc”或“cba”等。

            运行以下 1000 万次的一些性能时间。

            " mile ".trim(); 运行时间为 248 毫秒作为性能比较的参考实现。

            trim( "smiles", "s" ); 运行时间为 547 毫秒 - 大约是 java 的 String.trim() 方法的 2 倍。

            "smiles".replaceAll("s$|^s",""); 运行时间为 12,306 毫秒 - 大约是 java 的 String.trim() 方法的 48 倍。

            并使用已编译的正则表达式模式Pattern pattern = Pattern.compile("s$|^s"); pattern.matcher("smiles").replaceAll(""); 运行时间为 7,804 毫秒 - 大约是 java 的 String.trim() 方法的 31 倍。

            【讨论】:

            • "answer.length - trimChar.length - 1" 实际上
            • 没有真正优化。我不会用这个。
            • @BrettWidmeier 我想我已经写了这篇文章。 trim( "smiles", "les" ) 产生smitrim( "smiles", "s" ) 产生mile
            • 我想不出比这更低效的方法来解决这个问题。
            • @SoftwareMonkey 我同意这不是最有效的解决方案,但上下文很有帮助。在我的机器上,trim( "smiles", "s" ); 运行 1000 万次需要 547 毫秒," mile ".trim() 运行 1000 万次需要 248 毫秒。我的解决方案是 String.trim() 的 1/2,但仍然在大约半秒内完成了 1000 万次运行。
            【解决方案9】:

            我实际上会编写自己的小函数,通过使用普通的旧字符访问来解决问题:

            public static String trimBackslash( String str )
            {
                int len, left, right;
                return str == null || ( len = str.length() ) == 0 
                                       || ( ( left = str.charAt( 0 ) == '\\' ? 1 : 0 ) |
                       ( right = len > left && str.charAt( len - 1 ) == '\\' ? 1 : 0 ) ) == 0
                    ? str : str.substring( left, len - right );
            }
            

            这与 String.trim() 的行为类似,只是它使用 '\' 而不是空格。

            这是一种可行的替代方法,并且实际上使用了 trim()。 ;) 虽然它的效率不是很高,但它可能会在性能方面击败所有基于正则表达式的方法。

            String j = “\joe\jill\”;
            j = j.replace( '\\', '\f' ).trim().replace( '\f', '\\' );
            

            【讨论】:

              【解决方案10】:

              为第一个选项手工制作:

              public class Rep {
                  public static void main( String [] args ) {
                     System.out.println( trimChar( '\\' , "\\\\\\joe\\jill\\\\\\\\" )  ) ;
                     System.out.println( trimChar( '\\' , "joe\\jill" )  ) ;
                  }
                  private static String trimChar( char toTrim, String inString ) { 
                      int from = 0;
                      int to = inString.length();
              
                      for( int i = 0 ; i < inString.length() ; i++ ) {
                          if( inString.charAt( i ) != toTrim) {
                              from = i;
                              break;
                          }
                      }
                      for( int i = inString.length()-1 ; i >= 0 ; i-- ){ 
                          if( inString.charAt( i ) != toTrim ){
                              to = i;
                              break;
                          }
                      }
                      return inString.substring( from , to );
                  }
              }
              

              打印

              joe\jil

              joe\jil

              【讨论】:

              • 老大,在你的最后一行,它会是 return inString.substring( from , to + 1 );
              【解决方案11】:

              这就是你想要的:

              public static void main (String[] args) {
                  String a = "\\joe\\jill\\";
                  String b = a.replaceAll("\\\\$", "").replaceAll("^\\\\", "");
                  System.out.println(b);
              }
              

              $ 用于删除字符串末尾的序列。 ^ 用于在开头删除。

              作为替代方案,您可以使用以下语法:

              String b = a.replaceAll("\\\\$|^\\\\", "");
              

              | 的意思是“或”。

              如果您想修剪其他字符,只需调整正则表达式:

              String b = a.replaceAll("y$|^x", ""); // will remove all the y from the end and x from the beggining
              

              【讨论】:

              • 我认为你需要添加\\ ,即"\\y$|^\\x"
              • 你不是说"\\\\+$"吗?
              【解决方案12】:

              您可以使用来自 Apache Commons Lang 的 removeStartremoveEnd StringUtils

              【讨论】:

                【解决方案13】:

                编辑:由答案修改,仅替换第一个和最后一个“\”字符。

                System.err.println("\\joe\\jill\\".replaceAll("^\\\\|\\\\$", ""));
                

                【讨论】:

                  【解决方案14】:

                  似乎没有现成的使用 java api 可以做到这一点,但您可以编写一个方法来为您做到这一点。 这个link 可能有用

                  【讨论】:

                  • 确定它是:D 我的意思是一个修剪函数,它像问题所说的那样接受字符串
                  猜你喜欢
                  • 1970-01-01
                  • 2011-05-18
                  • 2016-08-22
                  • 1970-01-01
                  • 2013-09-24
                  • 2013-12-15
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多