How to replace the first occurrence of a substring found in a String using replaceFirst() in Java

2 Answers

0 votes
package javaapplication1;

public class JavaApplication1 {

    public static void main(String[] args) {

        try {

            String s = "wawa c c++ c# php";

            s = s.replaceFirst("wawa", "java");
            System.out.println(s);

        } catch (Exception e) {
            System.out.print(e.toString());
        }
    }
}

/*
             
run:
       
java c c++ c# php
    
 */

 



answered Nov 25, 2016 by avibootz
0 votes
package javaapplication1;

public class JavaApplication1 {

    public static void main(String[] args) {

        try {

            String s = "c c++ wawa c# php";

            s = s.replaceFirst("wawa", "java");
            System.out.println(s);

        } catch (Exception e) {
            System.out.print(e.toString());
        }
    }
}

/*
             
run:
       
c c++ java c# php
    
 */

 



answered Nov 25, 2016 by avibootz
...