h_moghaddasan
h_moghaddasan

Reputation: 27

How to convert a (Java source code) string to (HTML Entity (hex)) string in java?

I have a string with (java source format) and want to convert it to (Html entity(hex)) to use the target string in webview component. for more inf see ( http://www.fileformat.info/info/unicode/char/0068/index.htm ).

for example for word "hello":

1-source string is (java source):

"\u0068\u0065\u006C\u006C\u006F"

2-target must be (html entity):

 "hello"

I use replaceall(oldstr, newstr) function but it did not work because of "\" character- it is escape char in java.

can any one help me. thanks a lot.

Upvotes: 1

Views: 1968

Answers (1)

Joop Eggen
Joop Eggen

Reputation: 109547

s = s.replaceAll("\\\\u(....)", "&#x$1;");

In regex two backslashes represent the backslash itself. I Java string literals, a backslash is escaped itself too.

So we replace \u followed by a group () (being $1). This group contains four dots . whichs stands for any char except new line characters.

The HTML entity &...;is a numeric # hexadecimal x entity:

Java/JS       HTML
\u20ac   ->   €

Upvotes: 1

Related Questions