How to find and replace all occurrences of a substring in a string with Java

1 Answer

0 votes
public class FindAndReplaceAllOccurrencesOfASubstringInAString_Java {
    public static void main(String[] args) {
        String str = "c++ php go c python php phpphphp php rust";
        String word = "php";
        String replacewith = "JAVA";
 
        str = str.replaceAll(word, replacewith);
        
        System.out.println(str);
    } 
}


   
/*
run:
     
c++ JAVA go c python JAVA JAVAJAVAhp JAVA rust
    
*/

 



answered Oct 26, 2016 by avibootz
edited Aug 25, 2024 by avibootz
...