Where can I access StringUtils.replaceEach()?

Xonatron picture Xonatron · Feb 21, 2012 · Viewed 8.8k times · Source

The answer to Java escape HTML shows StringUtils.replaceEach() which runs similar to multiple StringUtils.replace()s, but accepting arrays of Strings instead of having to do each replace individually.

For example:

str = StringUtils.replace(str, "&", "&");
str = StringUtils.replace(str, "\"", """);
str = StringUtils.replace(str, "<", "&lt;");
str = StringUtils.replace(str, ">", "&gt;");

...becomes...

str = StringUtils.replaceEach(str,
    new String[]{"&", "\"", "<", ">"},
    new String[]{"&amp;", "&quot;", "&lt;", "&gt;"})

Much cleaner.

Where can I access StringUtils.replace()?

I have tried importing the following, to no avail:

  1. org.springframework.util.StringUtils
  2. org.apache.soap.util.StringUtils
  3. org.apache.axis.utils.StringUtils
  4. com.ibm.wsdl.util.StringUtils

They each have .replace(), of course.

Answer

Francisco Paulo picture Francisco Paulo · Feb 21, 2012

It believe you are looking for: org.apache.commons.lang3.StringUtils

You can find more about it here:

http://commons.apache.org/lang/api-3.1/org/apache/commons/lang3/StringUtils.html

And download the .jar here:

http://commons.apache.org/lang/