简体   繁体   中英

How to remove unwanted characters in a string

hi I am getting a json response from the server. I parsed it. but the String from the response looked like this

roominfo<br /><br /> <p><strong>Notifications and Fees:</strong><br /></p><p><ul><li>A resort fee is included in the total price displayed</li> </ul></p><p></p><p></p> <p>The following fees and deposits are charged by the property at time of service, check-in, or check-out. <ul><li>Valet parking fee: USD 30 per night (in/out privileges)</li><li>Pet fee: USD 45 per stay</li><li>Fee for wireless Inte.net in all public areas: USD 11.95 (for 24 hours, rates may vary)</li><li>Fee for in-room wireless Inte.net: USD 11.95 (for 24 hours, rates may vary)</li> </ul></p><p>The above list may not be comprehensive. Fees and deposits may not include tax and are subject to change. </p>

how to remove the unwanted characters from this string

If your unwanted character are HTML tags then use this

 String noHTMLString = htmlString.replaceAll("\\<.*?>","");

It uses a regular expression to remove any text that is enclosed with brackets.

there are some html tags which are supported by Html Class. see this http://commonsware.com/blog/Android/2010/05/26/html-tags-supported-by-textview.html

you can android Html class to remove or handle these tags. format would be like this.

Html.fromHtml(text).toString();

however there are several html tags which are not supported by html class so you can use webview to handle those tags.

Use jsoup parse

jsoup

for example you can do like this in java using jsoup:

String html = "<p>An <a href='http://example.com/'><b>example</b></a> link.</p>";
Document doc = Jsoup.parse(html);
Element link = doc.select("a").first();

String text = doc.body().text(); // "An example link"
String linkHref = link.attr("href"); // "http://example.com/"
String linkText = link.text(); // "example""

String linkOuterH = link.outerHtml(); 
// "<a href="http://example.com"><b>example</b></a>"
String linkInnerH = link.html(); // "<b>example</b>"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM