Kamalone
Kamalone

Reputation: 4115

How to parse a url from a String in android?

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

"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/abc from the String without using the subString method.

Upvotes: 20

Views: 32130

Answers (7)

Deyu瑜
Deyu瑜

Reputation: 544

I use this in Kotlin

fun findAllUrls(text: String): List<String> {
    return Regex(Patterns.WEB_URL.pattern()).findAll(text).map {
        it.value
    }.toList()
}

Upvotes: 5

mp501
mp501

Reputation: 666

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

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

Upvotes: 11

Al&#233;cio Carvalho
Al&#233;cio Carvalho

Reputation: 13647

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()]);
}

Upvotes: 45

Sunil Kumar Sahoo
Sunil Kumar Sahoo

Reputation: 53647

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

Upvotes: 6

inazaruk
inazaruk

Reputation: 74780

Updated:

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


Original:

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

Upvotes: 33

Steve Bergamini
Steve Bergamini

Reputation: 14600

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.

Upvotes: 0

fleetway76
fleetway76

Reputation: 1640

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.

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?

Upvotes: 0

Related Questions