如何从Android上的String获取URL [英] How to get URL from String on Android
本文介绍了如何从Android上的String获取URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从中提取网址。这是一个URL字符串http://mytest.com
。
我尝试使用EditText.getURLs,但它对我不起作用。
I tried to use EditText.getURLs but it didn't work for me.
EditText.setText("hi there this is a URL String http://stackoverflow.com");
EditText.getURLs.toString();
如何从EditText获取URL?
How can I get URL from EditText?
推荐答案
这是函数:
//Pull all links from the body for easy retrieval
private ArrayList pullLinks(String text) {
ArrayList links = new ArrayList();
String regex = "\\(?\\b(http://|www[.])[-A-Za-z0-9+&@#/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#/%=~_()|]";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(text);
while(m.find()) {
String urlStr = m.group();
if (urlStr.startsWith("(") && urlStr.endsWith(")")) {
urlStr = urlStr.substring(1, urlStr.length() - 1);
}
links.add(urlStr);
}
return links;
}
这篇关于如何从Android上的String获取URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文