【问题标题】:How to remove all occurrence of word in string Java如何删除字符串Java中所有出现的单词
【发布时间】:2023-03-10 08:40:01
【问题描述】:
String test = "This is my learning of java language and this is interesting";

我想选择这个字符串的第一个单词并删除它的所有出现,结果应该是

String test = "is my learning of java language and is interesting";

所以我可以知道“这个”这个词被删除了,它在给定的字符串中出现了 2 次。

在此先感谢...

【问题讨论】:

  • 好的,这就是你想要的。现在你的问题是什么?你做了什么?

标签: java string removeall


【解决方案1】:

这样:

String firstWord = test.split(" ")[0];
if (firstWord != null){
    String result = test.replaceAll(firstWord , "");
}

【讨论】:

    【解决方案2】:

    gtgaxiola 回答的一个版本:

    String test = "This is my learning of java language and this is interesting";
    String newTest = test.replaceAll("(?i)\\b" + test.split(" ")[0] + "\\b *", "").trim();
    

    或者,如果您的“单词”中可能包含特殊字符,那么这个功能会更加强大:

    String newTest = test.replaceAll("(?i)\\b" + Pattern.quote(test.split(" ")[0]) + "\\b *", "").trim();
    

    这会检查以确保要删除的单词位于单词边界上(即不是较大单词的一部分),并且还会删除被删除单词后面的所有空格字符。

    【讨论】:

      【解决方案3】:

      为简单起见,示例假设存在有效输入。

       11   public static String removeAllOccuranceOfFirst(String input) {
       12   
       13     String[] words = input.split(" ");
       14   
       15     String firstWord = words[0];
       16   
       17     StringBuilder result = new StringBuilder();
       18   
       19     for(int i=1; i<words.length; i++) {
       20   
       21        if(firstWord.equlaIgnorecase(words[i]) == false) {
       22           result.append(words[i]);
       23           result.append(" ");      
       24        }
       25     }
       26   
       27     return result.toString();
       28   }
      

      那么这里发生了什么。

      13 - 当我们将空格视为分隔符时,我们将输入拆分为单独的字符串(单词)。万一你有昏迷,分号程序会失败。 (让您正确的家庭作业:)

      15 - 我们选择在 arra 索引 0 下的第一个单词。 Java 数组索引从零开始。

      17 - 我们创建一个字符串生成器的实例,这个类专门用于字符串操作,因为 String 本身是不可变的,一旦创建就不能更改,必须创建一个新的。

      19 - 我们从索引 1 开始循环并遍历所有 wrod。

      21 - 我们比较当前 (i) 词不等于第一个词

      22 - 如果不相等,我们将其附加到我们的 StringBuilder。

      28 - 我们将 StringBuilder 实例转换为 String 并返回它。

      【讨论】:

        【解决方案4】:

        您可以使用(?i) 进行不区分大小写的匹配:

        例如:

            String test = "This is my learning of java language and this is interesting";
            String newTest = test.replaceAll("(?i)"+test.split(" ")[0], "").trim();
        

        【讨论】:

        • trim() 消除了前导和尾随空格,但对多个内部空格没有帮助。此外,这将删除出现的 "this" 不是完整的单词。 test.replaceAll("(?i)\\b"+test.split(" ")[0]+"\\b *", "").trim(); 怎么样
        【解决方案5】:

        这样的?

        test = test.replaceAll((test.split(" ")[0]), "");
        

        编辑:好的,不区分大小写使这更有趣。

        String[] words = test.split(" ");
        String firstword = words[0];
        String test = "";
        for(String word : words) {
            if(!word.equalsIgnoreCase(firstword)) {
                test += " " + word;
            }
        }
        

        我在浏览器中编写了这一切,所以它可能并不完美,但这应该会让事情顺利进行。

        【讨论】:

        • 区分大小写怎么样?
        • 请将split 参数更改为String。并解决第一条评论。
        • 使用 StringBuilder 创建你的字符串
        • @MarcoAcierno 这是完成所请求任务的众多方法之一。 StringBuilder 是另一个。
        • 不,我的意思是:使用当前代码创建 X 个字符串(每个 +=)。
        【解决方案6】:

        为 Cody S +1 另一种方式是这样

        String firstword =test.split(" ")[0];
        
        test=test.replaceAll(firstword, "");
        

        【讨论】:

        • 除了将char 文字更正为String 文字之外,这有何不同?
        • @Sotirios Delimanolis 我的意思是这里的风格不同
        • 把它放在一行而不是两行?你认为一个答案值得这样的区别吗?我不。它不会为手头的问题带来任何新的东西。
        • 顺便说一句,您可以随时编辑或删除您的答案。
        • 我没有删除它。如果我认为需要更新答案,请更新它
        【解决方案7】:
         test.replaceAll("This","").replaceAll("this", "")
        

        【讨论】:

          猜你喜欢
          • 2017-11-01
          • 2016-02-02
          • 1970-01-01
          • 2013-03-04
          • 1970-01-01
          • 1970-01-01
          • 2013-12-18
          • 1970-01-01
          • 2013-05-26
          相关资源
          最近更新 更多