2
\$\begingroup\$

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?

\$\endgroup\$

closed as off-topic by TheCoffeeCup, Brythan, Hosch250, tim, ferada Mar 7 '15 at 10:07

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. After the question has been edited to contain working code, we will consider reopening it." – TheCoffeeCup, Brythan, Hosch250, tim, ferada
If this question can be reworded to fit the rules in the help center, please edit the question.

8
\$\begingroup\$

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

  1. the specification says replace the ins in institute with JSE, your code though, replaces just the first ins in institute, not them all
  2. you replace the ins with JSS, and not JSE.
  3. 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

\$\endgroup\$
  • \$\begingroup\$ First of all, thank you very much that really helped. Also sorry for the typo I will just change it right now, I meant to say "it is NOT doing the job perfectly". \$\endgroup\$ – newmudhar Mar 6 '15 at 19:31
  • 2
    \$\begingroup\$ @newmudhar for future reference, broken code is off topic on Code Review and will likely be put on hold. If you need help making code work correctly, use Stack Overflow. \$\endgroup\$ – Devon Parsons Mar 6 '15 at 23:01
5
\$\begingroup\$
  • Everything inside curly braces should be indented by the same number of spaces, even within the outer class. In Java, it's usually four. You've already done it correctly with the conditionals, so have it done everywhere else.

  • s should be final as it's not meant to be modified. You should also give it a better name that describes its meaning. SIngle-character variable names are discouraged with the exception of for loop counter variables.

    final String originalString = "This is a java institute of insurance and insu.";
    
\$\endgroup\$
3
\$\begingroup\$

You don't need the contains() check. Just remove the if-else statement:

public class ExistanceAndReplace {

    public static void main(String[] args) {
        String s = "This is a java institute of insurance and insu.";
        String s1 = s.replaceFirst("ins", "JSS");
        System.out.println(s1);
    }

}

Also, you could just print the result immediately:

public class ExistanceAndReplace {

    public static void main(String[] args) {
        String s = "This is a java institute of insurance and insu.";
        System.out.println(s.replaceFirst("ins", "JSS"));
    }

}

Not really much of else to say, since it is a short program.

\$\endgroup\$
1
\$\begingroup\$

My weakness is the RegEx, so I kept trying and reached the below:

private static void replaceChars(String str, String toReplace, String replaceWith) {
    str = str.replaceAll(toReplace, replaceWith + toReplace.subSequence(3, toReplace.length()));
    System.out.println(str);
}

It is a method which makes it even dynamic.

\$\endgroup\$
  • \$\begingroup\$ I don't like how it prints the result instead of returning it. Try: private static String replaceChars(...) {...return str;} where str is what you have here. \$\endgroup\$ – TheCoffeeCup Mar 6 '15 at 23:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.