0

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 ?

soundswaste
  • 2,964
  • 3
  • 23
  • 40
  • 5
    \s doesn't just match space, it matches any whitespace character, including new lines (see http://stackoverflow.com/questions/225337/how-do-i-split-a-string-with-any-whitespace-chars-as-delimiters) - why not just put a space character in? – eldris Jul 17 '13 at 17:59

3 Answers3

2

You could just use

String [] parts = comment.split("\\ ");

instead of

String [] parts = comment.split("\\s");

as eldris said, "\s" is for every white-space character, so "\ ", for just the space character itself should do for you.

Doodad
  • 1,518
  • 1
  • 15
  • 20
2

I would suggest a different approach:

String noNewLines = "Hi there, click here http://www.google.com ok?";
String newLines = "Hi there, \r\nclick here \nhttp://www.google.com ok?";
// This is a String format with two String variables. 
// They will be replaced with the desired values once the "format" method is called.
String replacementFormat = "<a href=\"%s\">%s</a>";
// The first round brackets define a group with anything starting with
// "http(s)". The second round brackets delimit that group by a lookforward reference
// to whitespace. 
String pattern = "(http(s)?://.+?)(?=\\s)";
noNewLines = noNewLines.replaceAll(
        pattern,
        // The "$1" literals are group back-references. 
        // In our instance, they reference the group enclosed between the first
        // round brackets in the "pattern" String. 
        new Formatter().format(replacementFormat, "$1", "$1")
        .toString()
);
System.out.println(noNewLines);
System.out.println();
newLines = newLines.replaceAll(
        pattern,
        new Formatter().format(replacementFormat, "$1", "$1")
        .toString()
);
System.out.println(newLines);

Output:

Hi there, click here <a href="http://www.google.com">http://www.google.com</a> ok?

Hi there, 
click here 
<a href="http://www.google.com">http://www.google.com</a> ok?

This will replace all your http(s) links to an anchor reference, whether or not you have newlines (windows or *nix) in your text.

Edit

For best coding practices you should set the replacementFormat and pattern variables as constants (so, final static String REPLACEMENT_FORMAT and so on).

Edit II

Actually grouping the URl pattern isn't really necessary, as the whitespace lookahead is sufficient. But well, I'm leaving it as is, it works.

Mena
  • 47,782
  • 11
  • 87
  • 106
  • Could you tell me what's happening here : `new Formatter().format(replacementFormat, "$1", "$1")` ? Also, will successive newlines be preserved too ? – soundswaste Jul 17 '13 at 18:17
  • @chontamcslurpy done! See my comments in the answer. Edit: successive newlines should be preserved as well, yes. Try prepending "\n" to " ok?" for instance. – Mena Jul 17 '13 at 18:28
1

I would suggest following solution to your problem:

  1. First split by new line character
  2. For each line do processing that you have mentioned above
  3. Add all processed lines

That ways new line character will be retained and also you will be able to do in each line what you are currently doing.

Hope this helps.

Cheers !!

Sachin Thapa
  • 3,559
  • 4
  • 24
  • 42