Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
372 views
in Technique[技术] by (71.8m points)

java - Is there a quick way to recognize HTML ASCII codes in a String or TextView?

Here are some HTML ASCII Codes:

http://www.ascii.cl/htmlcodes.htm

I have a string that may look like "All in a hard day 's work"

What is the best way to replace that ascii code, with an apostrophe?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use Apache's StringEscapeUtils.escapeHtml(String) or StringEscapeUtils.unescapeHtml(String). This is found in the commons libraries.

If you need to preserve any HTML Markup, but just remove any ascii encoding, then you will have to construct a Map of the values you want to escape. It's an exercise in String manipulation, so it may be considered an 'ugly hack', but it will run quickly.

For example with some pseudo code, Create a Map<String, String>(), and populate it with the the value you want to replace as the Key, and the value to replace it with in the Value. Find the HTML ascii code in the document using a regular expression, look the ascii code up in your Map of replacements Replace the occurrence of the HTML ascii code with the text equivalent.

I will post some code over the weekend if I get a chance.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...