文本中的URL转超链接 @param text 文本 @return
(String text)
| 427 | * @return |
| 428 | */ |
| 429 | public static String urlToHyperlink(String text){ |
| 430 | // url的正则表达式 |
| 431 | Matcher matcher = pattern.matcher(text); |
| 432 | |
| 433 | String resultText = "";// (临时变量,保存转换后的文本) |
| 434 | int lastEnd = 0;// 保存每个链接最后一行的下标 |
| 435 | |
| 436 | while(matcher.find()){ |
| 437 | resultText += text.substring(lastEnd, matcher.start()); |
| 438 | resultText += "<a target=\"_blank\" href=\"" + matcher.group() + "\">" + matcher.group() + "</a>"; |
| 439 | lastEnd = matcher.end(); |
| 440 | } |
| 441 | resultText += text.substring(lastEnd); |
| 442 | return resultText; |
| 443 | } |
| 444 | |
| 445 | /** |
| 446 | * 是否为微信浏览器 |
no test coverage detected