Java 如何从android中的字符串解析url?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6384240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 05:29:26  来源:igfitidea点击:

How to parse a url from a String in android?

javaandroid

提问by Kamalone

I want to parse the url from a String in android. The example String is

我想从 android 中的字符串解析 url。示例字符串是

"This is a new message. The content of the message is in 'http://www.example.com/asd/abc' "

I want to parse the url http://www.example.com/asd/abcfrom the String without using the subString method.

我想http://www.example.com/asd/abc在不使用 subString 方法的情况下从 String解析 url 。

采纳答案by inazaruk

Updated:

更新:

You can use regular expression with Patterns.WEB_URLregular expression to find all urls in your text.

您可以使用正则表达式和Patterns.WEB_URL正则表达式来查找文本中的所有 url。



Original:

原来的:

You can use Uri.parse(String uriString)function.

您可以使用Uri.parse(String uriString)功能。

回答by fleetway76

You can just create a new URL passing the string as the argument to the constructor. Then you can use the various methods on the URL class to access the different parts of the URL.

您可以创建一个新的 URL,将字符串作为参数传递给构造函数。然后您可以使用 URL 类上的各种方法来访问 URL 的不同部分。

OK, I see that this isnt what you meant. You could probably use a regular expression, but using substring is the easiest method. Why dont you want to use it?

好的,我明白这不是你的意思。您可能可以使用正则表达式,但使用子字符串是最简单的方法。你为什么不想使用它?

回答by SBerg413

If the string will always be similar, a quick-fix would be to use a StringTokenizer on the single quote. Then you would simply take the 2nd token.

如果字符串总是相似的,一个快速的解决方法是在单引号上使用 StringTokenizer。然后你只需拿第二个令牌。

回答by Sunil Kumar Sahoo

Yes its possible. Try with the following code sample

是的,它可能。尝试使用以下代码示例

ArrayList retrieveLinks(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();
        char[] stringArray1 = urlStr.toCharArray();

        if (urlStr.startsWith("(") && urlStr.endsWith(")"))
        {

            char[] stringArray = urlStr.toCharArray(); 

            char[] newArray = new char[stringArray.length-2];
            System.arraycopy(stringArray, 1, newArray, 0, stringArray.length-2);
            urlStr = new String(newArray);
            System.out.println("Finally Url ="+newArray.toString());

        }
        System.out.println("...Url..."+urlStr);
        links.add(urlStr);
        }
        return links;
        }

Thanks Deepak

谢谢迪帕克

回答by Alécio Carvalho

Use this:

用这个:

public static String[] extractLinks(String text) {
    List<String> links = new ArrayList<String>();
    Matcher m = Patterns.WEB_URL.matcher(text);
    while (m.find()) {
        String url = m.group();
        Log.d(TAG, "URL extracted: " + url);
        links.add(url);
    }

    return links.toArray(new String[links.size()]);
}

回答by mp501

If you are parsing the links for the purpose of styling them, Linkify is an elegant solution:

如果您解析链接是为了给它们设置样式,Linkify 是一个优雅的解决方案:

descriptionTextView.setText("This text contains a http://www.url.com")
Linkify.addLinks(descriptionTextView, Linkify.WEB_URLS);

You can also change the default colour of the link:

您还可以更改链接的默认颜色:

descriptionTextView.setLinkTextColor(ContextCompat.getColor(getContext(),
                R.color.colorSecondary));

The result looks like this:

结果如下所示:

enter image description here

在此处输入图片说明