【问题标题】:How can I remove a substring from a given String?如何从给定的字符串中删除子字符串?
【发布时间】:2011-12-08 04:32:44
【问题描述】:

有没有一种简单的方法可以从 Java 中的给定 String 中删除子字符串?

示例:"Hello World!",删除 "o""Hell Wrld!"

【问题讨论】:

    标签: java string


    【解决方案1】:

    您可以轻松使用String.replace():

    String helloWorld = "Hello World!";
    String hellWrld = helloWorld.replace("o","");
    

    【讨论】:

      【解决方案2】:

      查看Apache StringUtils:

      • static String replace(String text, String searchString, String replacement) 替换另一个字符串中所有出现的字符串 字符串。
      • static String replace(String text, String searchString, String replacement, int max) 用另一个字符串替换一个字符串 较大的字符串,用于搜索字符串的第一个最大值。
      • static String replaceChars(String str, char searchChar, char replaceChar) 将字符串中出现的所有字符替换为 另一个。
      • static String replaceChars(String str, String searchChars, String replaceChars) 一次性替换字符串中的多个字符。
      • static String replaceEach(String text, String[] searchList, String[] replacementList) 替换所有出现的字符串 另一个字符串。
      • static String replaceEachRepeatedly(String text, String[] searchList, String[] replacementList) 替换所有出现的 另一个字符串中的字符串。
      • static String replaceOnce(String text, String searchString, String replacement) 用另一个字符串替换一个字符串 更大的字符串,一次。
      • static String replacePattern(String source, String regex, String replacement) 替换源字符串的每个子字符串 使用给定的替换匹配给定的正则表达式 Pattern.DOTALL 选项。

      【讨论】:

      • 刚刚对 replacePattern 进行了基准测试,它比运行自定义 Java 代码慢 6 倍。
      【解决方案3】:
      replace('regex', 'replacement');
      replaceAll('regex', 'replacement');
      

      在你的例子中,

      String hi = "Hello World!"
      String no_o = hi.replaceAll("o", "");
      

      【讨论】:

        【解决方案4】:

        您应该查看StringBuilder/StringBuffer,它允许您在指定的offset 处删除、插入、替换字符。

        【讨论】:

          【解决方案5】:

          你也可以使用guava'sCharMatcher.removeFrom函数。

          示例:

           String s = CharMatcher.is('a').removeFrom("bazaar");
          

          【讨论】:

            【解决方案6】:

            你可以使用 StringBuffer

            StringBuffer text = new StringBuffer("Hello World");
            text.replace( StartIndex ,EndIndex ,String);
            

            【讨论】:

              【解决方案7】:

              这对我很有用。

              String hi = "Hello World!"
              String no_o = hi.replaceAll("o", "");
              

              或者你可以使用

              String no_o = hi.replace("o", "");
              

              【讨论】:

                【解决方案8】:
                private static void replaceChar() {
                    String str = "hello world";
                    final String[] res = Arrays.stream(str.split(""))
                            .filter(s -> !s.equalsIgnoreCase("o"))
                            .toArray(String[]::new);
                    System.out.println(String.join("", res));
                }
                

                如果你有一些复杂的逻辑来过滤字符,只是另一种方法而不是replace()

                【讨论】:

                  【解决方案9】:

                  这是从给定字符串中删除所有子字符串的实现

                  public static String deleteAll(String str, String pattern)
                  {
                      for(int index = isSubstring(str, pattern); index != -1; index = isSubstring(str, pattern))
                          str = deleteSubstring(str, pattern, index);
                  
                      return str;
                  }
                  
                  public static String deleteSubstring(String str, String pattern, int index)
                  {
                      int start_index = index;
                      int end_index = start_index + pattern.length() - 1;
                      int dest_index = 0;
                      char[] result = new char[str.length()];
                  
                  
                      for(int i = 0; i< str.length() - 1; i++)
                          if(i < start_index || i > end_index)
                              result[dest_index++] = str.charAt(i);
                  
                      return new String(result, 0, dest_index + 1);
                  }
                  

                  isSubstring()方法的实现是here

                  【讨论】:

                    【解决方案10】:
                    replaceAll(String regex, String replacement)
                    

                    以上方法将有助于得到答案。

                    String check = "Hello World";
                    check = check.replaceAll("o","");
                    

                    【讨论】:

                      【解决方案11】:

                      您也可以使用 Substring 替换现有字符串:

                      var str = "abc awwwa";
                      var Index = str.indexOf('awwwa');
                      str = str.substring(0, Index);
                      

                      【讨论】:

                        【解决方案12】:

                        如果你知道开始和结束索引,你可以使用这个

                        string = string.substring(0, start_index) + string.substring(end_index, string.length());
                        

                        【讨论】:

                          猜你喜欢
                          • 2021-03-12
                          • 2020-05-01
                          • 2015-10-02
                          • 1970-01-01
                          • 2019-09-08
                          • 1970-01-01
                          相关资源
                          最近更新 更多