简体   繁体   English

如何删除字符串Java中所有单词的出现

[英]How to remove all occurrence of word in string Java

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

I want to pick first word of this string and remove all its occurences and result should be 我想选择该字符串的第一个单词并删除所有出现的单词,结果应为

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

So i could know that "This" word is removed and it appeared 2 times in given string. 因此,我知道“ this”一词已删除,并且在给定字符串中出现了2次。

Thanks in Advance... 提前致谢...

You may use (?i) for case-insensitive matches: 您可以将(?i)用于不区分大小写的匹配:

For example: 例如:

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

Something like this? 像这样吗

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

EDIT: Ok, case insensitivity makes this a bit more interesting. 编辑:好的,不区分大小写使它变得更有趣。

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

I'm writing this all in-browser, so it might not be perfect, but that should get the ball rolling. 我正在浏览器中编写所有内容,因此它可能并不完美,但这应该可以解决问题。

For sake of simplicity the example assume that there is valid input. 为了简单起见,该示例假定存在有效输入。

 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   }

So whats is going on here. 所以这是怎么回事。

13 - we take the input and split into separate strings (words) as we treat the space as delimiter. 13-当我们将空格视为定界符时,我们将输入并分成单独的字符串(单词)。 Not in case you have coma, semicolon the program will fail. 万一您有昏迷,分号程序将失败。 (a home work for you to make it correct:) (您需要做的一项家庭作业:)

15 - We pick up the first words that is under index zero of arra. 15-我们选择arra索引零下的第一个单词。 Java arrays indexes starts from zero. Java数组索引从零开始。

17 - we create an instance of string builder, this class is dedicated to string manipulation as String itself is immutable and once create can not be changed and has to be created new one. 17-我们创建了一个字符串生成器的实例,该类专用于字符串操作,因为String本身是不可变的,一旦创建就无法更改,必须创建一个新的字符串。

19 - we start a loop from index one and iterate through all wrods. 19-我们从索引1开始循环,并遍历所有错误。

21 - we compare the current (i) words is not equal to first word 21-我们比较当前(i)个单词不等于第一个单词

22 - if is not equal we append it to our StringBuilder. 22-如果不相等,则将其附加到StringBuilder中。

28 - we transform our StringBuilder instance into String and return it. 28-我们将StringBuilder实例转换为String并返回它。

A version of gtgaxiola's answer: 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();

Or this one is even more robust if there's a possibility of special characters in your "word": 或者,如果您的“单词”中可能有特殊字符,则此功能甚至更强大:

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

This checks to make sure the words being removed are on word boundaries (ie not part of a larger word), and it also removes any space characters following a word that gets removed. 这将检查以确保要删除的单词在单词边界上(即不是较大单词的一部分),并且还删除了要删除的单词之后的所有空格字符。

 test.replaceAll("This","").replaceAll("this", "")

Like that: 像那样:

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

+1 for Cody S an other way is this +1为Cody S的另一种方式是

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

test=test.replaceAll(firstword, "");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM