Java String search and replace I would like to search a String, and for a certain word, I would like to change only the first 3 letters

For example:


  This is a java institute which insures that you are in that institute. ins!


I want to change the first 3 letter of institute (ins) to JSE, so the result will be:


  This is a java JSEtitute which insures that you are in that JSEtitute. ins!


I have the following, but it is NOT doing the job perfectly:

public class ExistanceAndReplace {

public static void main(String[] args) {

String s = "This is a java institute of insurance and insu.";

if (s.contains("institute")) {
    String s1 = s.replaceFirst("ins", "JSS");
    System.out.println(s1);


}else{
    System.out.println("not found!");
}

}

}


Any suggestions?
This code of yours is not working, despite your assurances that it is. It replaces just the first occurrence of 'ins', and that should be obvious since you call the replaceFirst method. The example sentence you use in the code is not the same as the sentence in your description. The one in the description will fail your code:


This is a java institute which insures that you are in that institute. ins!



Your code will produce:


This is a java JSStitute which insures that you are in that institute. ins!



and it should produce:


This is a java JSEtitute which insures that you are in that JSEtitute. ins!



There are three problems


the specification says replace the ins in institute with JSE, your code though, replaces just the first ins in institute, not them all
you replace the ins with JSS, and not JSE.
you don't check for the beginning of the word... you will find ins in "dustbins".


These small details are things you have to fix, and show a low attention to detail.

Of course, a more advanced solution would be:

s.replaceAll("\\bins(?=titute\\b)", "JSE");


You can see that in action here in Ideone