abidkhan303
abidkhan303

Reputation: 1791

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.

Thanks in Advance...

Upvotes: 0

Views: 4777

Answers (7)

Houssam Badri
Houssam Badri

Reputation: 2509

Like that:

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

Upvotes: 0

ajb
ajb

Reputation: 31699

A version of gtgaxiola's answer:

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 (i.e. not part of a larger word), and it also removes any space characters following a word that gets removed.

Upvotes: 1

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. 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. Java arrays indexes starts from zero.

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.

19 - we start a loop from index one and iterate through all wrods.

21 - we compare the current (i) words is not equal to first word

22 - if is not equal we append it to our StringBuilder.

28 - we transform our StringBuilder instance into String and return it.

Upvotes: 1

gtgaxiola
gtgaxiola

Reputation: 9331

You may use (?i) for case-insensitive matches:

For example:

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

Upvotes: 2

Cody S
Cody S

Reputation: 4824

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.

Upvotes: 1

Asif Bhutto
Asif Bhutto

Reputation: 3994

+1 for Cody S an other way is this

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

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

Upvotes: -1

Olesya Razuvayevskaya
Olesya Razuvayevskaya

Reputation: 1168

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

Upvotes: 0

Related Questions