2

An obvious way to handle String search and replace is

String s = "This will go over well!";
s = s.replace("go", "not go");

I wonder .. can the same be done using regex package? Are there benefits from using it?

Can something along the lines of s/go/not go/g exist?

1 Answer 1

1

I wonder .. can the same be done using regex package? Are there benefits from using it?

Please look at using String#replaceAll(String regex, String replacement) instead of String#replace(...) since this uses a regular expression for the first parameter. The String API will tell you more about this. This does give some added flexibility and power but with the price (I believe -- I have not profiled this) of slower execution time.

Can something along the lines of s/go/not go/g exist?

I'm not sure I understand what you're asking here. Can you please clarify this a bit?

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for responding. Your answer is perfect. Will accept in 5..4 min

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.