How to replace only first occurrences of given string with new string and returns the new string in Java

1 Answer

0 votes
package javaapplication1;

public class JavaApplication1 {
    
    public static void main(String[] args) {
  
        String s = "abc xyz abc";

        s = s.replaceFirst("ab", "WWW");
        System.out.println(s);
    }
}
   
/*
   
run:
   
WWWc xyz abc
   
*/

 



answered Oct 25, 2016 by avibootz
...