In the project (news content System), the use of Baidu editor content stored in the database (including HTML tags), how to use the tool to convert text (without HTML tags)!

Source: Internet
Author: User
Tags stringbuffer

First import jar packages into your project: Jsoup-1.7.2.jar (I'm using this version of the jar package)

Tool class: Htmlconverttext.java

Import Org.jsoup.Jsoup;
Import org.jsoup.nodes.Document;
Import org.jsoup.nodes.Element;
Import org.jsoup.select.Elements;

public class Htmlconverttext {public
	static string Converttext (string text) {
		if (text!= null) {
			Document doc = Jsoup.parse (text);
			
			Elements links = doc.getelementsbytag ("P");
			StringBuffer buffer =new stringbuffer ();
	    	for (Element link:links) {
	    		buffer.append (Link.text (). Trim ());
			Return buffer.tostring (). Trim (); 
		return null;
	}
	public static string substrings (String Text,int num) {
		if (text!= null) {return
			text.substring (0, num). Trim ();
		}
		return null;
	}
}

Using this tool class, you can convert content (with HTML tags) to text to facilitate the display of a certain number of Chinese characters in the foreground.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.