funtik
funtik

Reputation: 1826

StringUtils.replaceEach: Replace only separate words but not substring

I need to use Apache's StringUtils.replaceEach() method for replacing set of strings. However, this methods also replaces substrings in the word. I know I can use replaceAll method with \b regex. But I wanted to know if there's a way to tell StringUtils to not replace substrings.

Thanks in advance,

Upvotes: 0

Views: 1263

Answers (3)

Klugscheißer
Klugscheißer

Reputation: 1614

Don't think there's a flag for StringUtils but one possible hack would be to surround your replacement set with spaces. So instead of "foo" -> "bar", you'd have " foo " -> " bar ". This sadly isn't very helpful if your input may have new lines etc. instead of just spaces between words.

Upvotes: 0

Sai prateek
Sai prateek

Reputation: 11896

You can try StringUtils.repeat(), more info can be found to github.

Upvotes: 0

MartyIX
MartyIX

Reputation: 28648

StringUtils class is envisioned to operate on strings in general. Yet, you can have a look at the source code:

https://github.com/apache/commons-lang/blob/master/src/main/java/org/apache/commons/lang3/StringUtils.java#L5749

There is also WordUtils class but it also does not contain what you need.

Upvotes: 0

Related Questions