Jessy Jameson
Jessy Jameson

Reputation: 207

retrieve some text from url string and save to a separate string

I have the following url and i want to retrieve the part 124301. this number is different every time. how can i get it and save it to an other string

 String pageurl = "http://www.examplewebsite.com/article/124301/blabalala/blalalalaa"

Upvotes: 1

Views: 293

Answers (2)

Kayhan Asghari
Kayhan Asghari

Reputation: 2844

if http://www.examplewebsite.com/article/ part is fixed, so you can get that simply this way:

String pageurl2 = pageurl.replace("http://www.examplewebsite.com/article/", "");
String result = (pageurl2.split("/"))[0];

also there you can use regular expressions too.

Upvotes: 2

Upgradingdave
Upgradingdave

Reputation: 13056

I haven't tested, but something like this should work:

 String pageurl = "http://www.examplewebsite.com/article/124301/blabalala/blalalalaa"
  Pattern pattern = Pattern.compile("\\/article\\/(\\d+)\\/");
  Matcher matcher = pattern.matcher(pageurl);

    if(matcher.find()){

        return matcher.group(1);

    } else {

        return null; //not found

    }

Upvotes: 2

Related Questions