关于超链接的字符串替换...
"12345678[link]www.csdn.net[/link]AAAAAAAAAAAAAAAA[link]www.google.com[/link]BBBBBB ";
有上面这样一个字符串,小弟想把它替换成:
"12345678 <a href= 'http://www.csdn.net '> www.csdn.net </a> AAAAAAAAAAAAAAAA <a href= 'http://www.google.com '> www.google.com[/link]BBBBBB ";
各位大虾,看看该怎么做 !?
这是小弟的代码,请各位指教...
public String replaceAllLink(String contents){
while(contents.indexOf( "[link] ")!=-1){
String frontStr=contents.substring(0,contents.indexOf( "[link] "));
String backStr=contents.substring(contents.indexOf( "[/link] ")+7,contents.length());
String src=contents.substring(content.indexOf( "[link] ")+6,contents.indexOf( "[/link] "));
String aStr= " <a href= 'http:// "+src+ " '> "+src+ " </a> ";
System.out.println( "frontStr: "+frontStr);
System.out.println( "frontStr: "+backStr);
System.out.println( "aStr: "+aStr);
contents=contents.replaceAll( "[link].*[/link] ",aStr);
System.out.println( "contents: "+frontStr+aStr+backStr);
}
}
------解决方案--------------------System.out.println( "12345678[link]www.csdn.net[/link]AAAAAAAAAAAAAAAA[link]www.google.com[/link]BBBBBB ".replaceAll( "\\[link\\](.+?)\\[/link\\] ", " <a href=\ "$1\ "> $1 </a> "));