I have java program, which will receive plain text from server. The plain text may contain URLs. Is there any Class in Java library to convert plain text to HTML text? Or any other library? If there are not then what is the solution?
-
1pretty simple to write your own – pstanton Feb 27 '11 at 18:19
-
6Plain text, by definition, can not contain hyperlinks. – phihag Feb 27 '11 at 18:20
-
2@phihag I'd say this is plain text: "www.google.com" – atamanroman Feb 27 '11 at 18:26
-
1You should define your requirements more precisely. What does the plain text look like? What do you expect the resulting HTML to look like? – JB Nizet Feb 27 '11 at 18:28
-
5Don't be fool. This is pretty simple. atamanroman is right. If i write and save `http://www.google.com` in notepad and save it as text, that is a plain text. And As it contain a hyperlink. And i want to render it in `JEditorPan` as a link. That's all. – Shaiful Feb 27 '11 at 18:36
-
http://www.java2s.com/Code/Java/Swing-JFC/JTextPaneHighlightExample.htm perhaps? – Cratylus Feb 27 '11 at 18:47
-
@user384706 . thats not plain text. that's are html text. – Shaiful Feb 27 '11 at 18:49
-
@Shaiful: strictly speaking `http://www.google.com/` is not a [hyperlink](http://en.wikipedia.org/wiki/Hyperlink), it's [an URI](http://en.wikipedia.org/wiki/Uniform_Resource_Identifier). You can easily convert an URI to a hyperlink, if you're working in a format that supports hyperlinks, but plain text doesn't ("a hyperlink (or link) is a reference to a document that the reader can **directly follow**", emphasis mine). – Joachim Sauer Feb 28 '11 at 07:53
-
1Upvoting to counter all the downvoting haters on this post. The question is perfectly reasonable if not ideally posed - I know so because I landed here while searching for an answer to the same. The question is basically: is there a public API somewhere that does what the code in Daniel's answer does. Give the OP a break. – Rhubarb Aug 05 '13 at 18:39
-
JSoup has a good solution, see: https://stackoverflow.com/a/44064930/381161 – Malcolm Smith May 31 '17 at 10:58
6 Answers
You should do some replacements on the text programmatically. Here are some clues:
- All Newlines should be converted to
"<br>\n"
(The \n for better readability of the output). - All CRs should be dropped (who uses DOS encoding anyway).
- All pairs of spaces should be replaced with
" "
- Replace "<" with
"<"
- Replace "&" with
"&"
- All other characters < 128 should be left as they are.
- All other characters >= 128 should be written as
"&#"+((int)myChar)+";"
, to make them readable in every encoding. - To autodetect your links, you could either use a regex like
"http://[^ ]+"
, or"www.[^ ]"
and convert them like JB Nizet said. to"<a href=\""+url+"\">"+url+"</a>"
, but only after having done all the other replacements.
The code to do this looks something like this:
public static String escape(String s) {
StringBuilder builder = new StringBuilder();
boolean previousWasASpace = false;
for( char c : s.toCharArray() ) {
if( c == ' ' ) {
if( previousWasASpace ) {
builder.append(" ");
previousWasASpace = false;
continue;
}
previousWasASpace = true;
} else {
previousWasASpace = false;
}
switch(c) {
case '<': builder.append("<"); break;
case '>': builder.append(">"); break;
case '&': builder.append("&"); break;
case '"': builder.append("""); break;
case '\n': builder.append("<br>"); break;
// We need Tab support here, because we print StackTraces as HTML
case '\t': builder.append(" "); break;
default:
if( c < 128 ) {
builder.append(c);
} else {
builder.append("&#").append((int)c).append(";");
}
}
}
return builder.toString();
}
However, the link conversion has yet to be added. If someone does it, please update the code.

- 27,718
- 20
- 89
- 133
-
-
Sorry, I hadn't thought about this simple solution. The state machine was for the inverse case, of converting HTML to plain text, which is a bit more difficult. – Daniel Feb 27 '11 at 19:04
-
1You can skip the "encode chars > 128", if you simply specify the correct encoding in a your HTML. – Joachim Sauer Feb 28 '11 at 07:55
-
-
1good answer- I basically copy pasted it into my code. Surprising that there isn't a public API in one of the utility libs like apache commons for this – Rhubarb Aug 05 '13 at 18:41
-
3Note that the *escaping* portions of the code above can be performed by calling escapeHtml4 - https://commons.apache.org/proper/commons-lang/javadocs/api-3.4/org/apache/commons/lang3/StringEscapeUtils.html#escapeHtml4(java.lang.String) – Ken Lin Jul 28 '15 at 16:44
-
@Rhubarb: What was the code for? Always nice to know where code I wrote is used :) – Daniel Jun 20 '19 at 14:25
-
I found a solution using pattern matching. Here is my code -
String str = "(?i)\\b((?:https?://|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:\'\".,<>?«»“”‘’]))";
Pattern patt = Pattern.compile(str);
Matcher matcher = patt.matcher(plain);
plain = matcher.replaceAll("<a href=\"$1\">$1</a>");
And Here are the input and output -
Input text is variable plain
:
some text and then the URL http://www.google.com and then some other text.
Output :
some text and then the URL <a href="http://www.google.com">http://www.google.com</a> and then some other text.

- 5,643
- 5
- 38
- 41
Just joined the coded from all answers:
private static String txtToHtml(String s) {
StringBuilder builder = new StringBuilder();
boolean previousWasASpace = false;
for (char c : s.toCharArray()) {
if (c == ' ') {
if (previousWasASpace) {
builder.append(" ");
previousWasASpace = false;
continue;
}
previousWasASpace = true;
} else {
previousWasASpace = false;
}
switch (c) {
case '<':
builder.append("<");
break;
case '>':
builder.append(">");
break;
case '&':
builder.append("&");
break;
case '"':
builder.append(""");
break;
case '\n':
builder.append("<br>");
break;
// We need Tab support here, because we print StackTraces as HTML
case '\t':
builder.append(" ");
break;
default:
builder.append(c);
}
}
String converted = builder.toString();
String str = "(?i)\\b((?:https?://|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:\'\".,<>?«»“”‘’]))";
Pattern patt = Pattern.compile(str);
Matcher matcher = patt.matcher(converted);
converted = matcher.replaceAll("<a href=\"$1\">$1</a>");
return converted;
}

- 499
- 5
- 4
-
For URL like `www.stackoverflow.com` should be converted to `http://www.stackoverflow.com` – Rubens Mariuzzo Sep 28 '12 at 07:03
Use this
public static String stringToHTMLString(String string) { StringBuffer sb = new StringBuffer(string.length()); // true if last char was blank boolean lastWasBlankChar = false; int len = string.length(); char c; for (int i = 0; i < len; i++) { c = string.charAt(i); if (c == ' ') { // blank gets extra work, // this solves the problem you get if you replace all // blanks with , if you do that you loss // word breaking if (lastWasBlankChar) { lastWasBlankChar = false; sb.append(" "); } else { lastWasBlankChar = true; sb.append(' '); } } else { lastWasBlankChar = false; // // HTML Special Chars if (c == '"') sb.append("""); else if (c == '&') sb.append("&"); else if (c == '<') sb.append("<"); else if (c == '>') sb.append(">"); else if (c == '\n') // Handle Newline sb.append("<br/>"); else { int ci = 0xffff & c; if (ci < 160) // nothing special only 7 Bit sb.append(c); else { // Not 7 Bit use the unicode system sb.append("&#"); sb.append(new Integer(ci).toString()); sb.append(';'); } } } } return sb.toString(); }

- 5,977
- 2
- 24
- 25
In Android application I just implemented HTMLifying of a content ( see https://github.com/andstatus/andstatus/issues/375 ). Actual transformation was done in literary 3 lines of code using Android system libraries. This gives an advantage of using better implementation at each subsequent version of Android libraries.
private static String htmlifyPlain(String textIn) {
SpannableString spannable = SpannableString.valueOf(textIn);
Linkify.addLinks(spannable, Linkify.WEB_URLS);
return Html.toHtml(spannable);
}

- 2,391
- 3
- 21
- 26
If your plain text is a URL (which is different from containing a hyperlink, as you wrote in your question), then transforming it into a hyperlink in HTML is simply done by
String hyperlink = "<a href='" + url + "'>" + url + "</a>";

- 678,734
- 91
- 1,224
- 1,255
-
That's not String. That is total plain text with URL. `some text and then the URL http://www.google.com and then some other text.` – Shaiful Feb 27 '11 at 18:47
-
Actually, if you render that as HTML it will be a link. You should do a bit of reading about HTML. – Tanner Feb 27 '11 at 19:07
-
@Tanner - Did you mean, if i set the htmltext of JEditorPan to a plain text, then all string start with http://.... will be a link? – Shaiful Feb 27 '11 at 19:12
-
No. Your JEditorPane needs to be set the html. It then will render any text as HTML. That means it will accept and render HTML.
etc. For it to display a link you need to format your URL to a link which can be done using the format JB Nizet said. text
– Tanner Feb 27 '11 at 19:17