1

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.

Community
  • 1
  • 1
Xonatron
  • 15,622
  • 30
  • 70
  • 84

1 Answers1

3

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/

Francisco Paulo
  • 6,284
  • 26
  • 25
  • thanks so much. Unfortunately I cannot add new `.jar`s to this project as I am not in control of that. – Xonatron Feb 21 '12 at 18:23