I am trying to convert all links in a given string to clickable a
tags using the following code :
String [] parts = comment.split("\\s");
String newComment=null;
for( String item : parts ) try {
URL url = new URL(item);
// If possible then replace with anchor...
if(newComment==null){
newComment="<a href=\"" + url + "\">"+ url + "</a> ";
}else{
newComment=newComment+"<a href=\"" + url + "\">"+ url + "</a> ";
}
} catch (MalformedURLException e) {
// If there was an URL that was not it!...
if(newComment==null){
newComment = item+" ";
}else{
newComment = newComment+item+" ";
}
}
It works fine for
Hi there, click here http://www.google.com ok?
converting it to
Hi there, click here <a href="http://www.google.com">http://www.google.com</a> ok?
But when the string is this :
Hi there, click
here http://www.google.com
ok?
its still converting it to :
Hi there, click here <a href="http://www.google.com">http://www.google.com</a> ok?
Whereas I want the final result to be :
Hi there, click
here <a href="http://www.google.com">http://www.google.com</a>
ok?
I think its including the newline character also while making the split.
How do I preserve the newline character in this case ?