2013-07-17 52 views
0

我想所有的鏈接轉換給定的字符串使用下面的代碼可點擊a標籤:斯普利特空間,但不換行

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+" "; 
    } 
} 

它工作正常的

Hi there, click here http://www.google.com ok? 

將其轉換爲

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

但當字符串是這樣的:

Hi there, click 

here http://www.google.com 

ok? 

它還是將其轉換爲:

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? 

我認爲它包括換行符也同時使分裂。

如何在這種情況下保留換行符?

+5

\ s不僅僅匹配空格,它匹配任何空白字符,包括新行(請參閱http://stackoverflow.com/questions/225337/how-do-i-split-a-string-with-any -whitespace-chars-as-delimiters) - 爲什麼不把空格字符放在裏面? – eldris

+0

哦謝謝!我不知道:) – soundswaste

回答

2

我會建議不同的方法:

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); 

輸出:

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? 

這將取代所有的HTTP(S)的鏈接錨參考,無論是否有換行符(窗口或* nix)。

編輯

爲了獲得最佳的編碼實踐,你應該設置replacementFormatpattern變量爲常數(所以,final static String REPLACEMENT_FORMAT等)。

編輯II

其實分組的URL模式是不是真的有必要,因爲空白前瞻就足夠了。但是,我現在離開它,它工作。

+0

你能告訴我這裏發生了什麼:'new Formatter().format(replacementFormat,「$ 1」,「$ 1」)'? 另外,還會保留連續的換行符嗎? – soundswaste

+0

@chontamcslurpy完成!在答案中看到我的評論。編輯:連續的換行符也應該保留,是的。嘗試將「\ n」預先添加到「好嗎?」例如。 – Mena

1

我建議以下解決問題的方法:

  1. 通過新行字符首先分裂
  2. 對於每一行做加工,你在上面
  3. 提到將所有的加工線

這種方式將保留新的行字符,並且您將能夠在每行中執行您目前正在執行的操作。

希望這會有所幫助。

乾杯!

2

你可以只使用

字符串[]份= comment.split( 「\\」);

代替

字符串[]份= comment.split( 「\\ S」);

正如eldris所說,「\ s」適用於每個空格字符,所以「\」,因爲空格字符本身應該爲你做。