How can I replace the symbol?

efkw
efkw used Ask the Experts™
on
Hi all,

How can I replace the symbol: ', ", \, & ... etc. with unicode?

I tried this code but it doesn't work with unicode entries:

text1=text1.replaceAll("&", "&");

all unicode in source code will be shown:

 

how to do it?

Thanks~

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
I think you mean you want to replace certain characters with their HTML entity code. Here is the method I use:

  public static String filter(String value) {
    if (value == null) return (null);
    char content[] = new char[value.length()];
    value.getChars(0, value.length(), content, 0);
    StringBuffer result = new StringBuffer(content.length + 50);
    for (int i = 0; i < content.length; i++) {
      switch (content[i]) {
        case '<':
          result.append("&lt;");
          break;
        case '>':
          result.append("&gt;");
          break;
        case '&':
          result.append("&amp;");
          break;
        case '"':
          result.append("&quot;");
          break;
        case '\'':
          result.append("&#39;");
          break;
        default:
          result.append(content[i]);
      }
    }
    return (result.toString());
  }

Hope that helps,

-Pat

Author

Commented:
thanks!

:)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial