MakingMoneyWithAndroid
MakingMoneyWithAndroid

Reputation: 493

Extract substring from string Android

I have string like this:

<a hidden="true" href="xxx" class=" zt-uie-session      </a>

How to extract href value in Android? It's string, not some object.

Upvotes: 1

Views: 201

Answers (3)

ZeroOne
ZeroOne

Reputation: 9117

you can use Jsoup library for easily extract html attributes

Document doc = Jsoup.parse("<a href='link' />");
Element link = doc.select("a").first();
return link.attr("href"); //xxx

Doc here

Upvotes: 0

Divyang Panchal
Divyang Panchal

Reputation: 1909

Try below code it is working fine

        String str = "<a hidden=\"true\" href=\"xxx\" class=\" zt-uie-session></a>";
        Matcher m = Pattern.compile(" (?:href)=\"([^\"]+)").matcher(str);

        while (m.find()) {
            String result = m.group(1);
            Log.d("HREF",": "+result); //result is "xxx" 
        }

Upvotes: 0

Raman Sahasi
Raman Sahasi

Reputation: 31841

You can use this regex to find href value:

href="([^"]*)"

DEMO

Matcher m = Pattern.compile("href=\"([^\"]*)\"").matcher("<a hidden=\"true\" href=\"xxx\" class=\" zt-uie-session      </a>");
if (m.find())
    System.out.println(m.group(1));

Output

xxx

Upvotes: 2

Related Questions