Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,950 questions

51,892 answers

573 users

How to remove all occurrences of word from a string in Java

3 Answers

0 votes
import java.util.regex.Pattern;

public class RemoveAllOccurrencesOfWordFromString_Java {
    public static void main(String[] args) {
        String s = "rust java c c++ java c# java golang python";
        String remove = "java";
        
        Pattern regex = Pattern.compile(remove);
        s = regex.matcher(s).replaceAll("");

        System.out.println(s);
    }
}


/*
run:

rust  c c++  c#  golang python

*/

 



answered Oct 13, 2024 by avibootz
0 votes
public class RemoveAllOccurrencesOfWordFromString_Java {
    public static void main(String[] args) {
        String s = "rust java c c++ java c# java golang python";
        String remove = "java";
        
        s = s.replaceAll(remove, "");

        System.out.println(s);
    }
}


/*
run:

rust  c c++  c#  golang python

*/

 



answered Oct 13, 2024 by avibootz
0 votes
public class Main {
    static String removeWord(String str, String word) {
        String[] words = str.toLowerCase().split(" ");
        String new_str = "";
 
        for (String s : words) {
            if (!s.equals(word)) {
                new_str += s + " ";
            }
        }
 
        return new_str;
    }
    public static void main(String[] args) {
        String s = "rust java c c++ java c# java golang python";
         
        s = removeWord(s, "java");
 
        System.out.println(s);
    }
}
 
 
/*
run:
 
rust c c++ c# golang python 
 
*/

 



answered Feb 2, 2025 by avibootz

Related questions

1 answer 148 views
1 answer 133 views
2 answers 104 views
2 answers 111 views
1 answer 104 views
...