0
http://m.facebook.com/messages/1245/abc
http://m.facebook.com/messages/12/
http://m.facebook.com/messages/5/
http://m.facebook.com/messages/particular_String/abcd/Trigma

So these are the urls and i would like to fetch the 1245 from first url 12 from second 5 from third and particular_string from fourth.how can i do this?

Shuchita
  • 13
  • 1
  • 6

3 Answers3

1

Use String.split to get all the substrings in the string that are between the '/' delimeter as an array, like so

myString.split("/")

from "http://m.facebook.com/messages/1245/abc" you will get an array of

http:

m.facebook.com
messages
1245
abc

then get the one which is 1245

svarog
  • 9,477
  • 4
  • 61
  • 77
1

Well if all urls have the same base, namely-

http://m.facebook.com/messages/

I would replace that like

urlString = urlString.replace ("http://m.facebook.com/messages/","");

Then tokenize the remaining portion to get the first part.

StringTokenizer tokenizedString = new StringTokenizer(urlString, "/");

String requiredString = tokenizedString.nextToken();

Tsietsi
  • 51
  • 3
  • 6
0

Probably you may use String.contains() method to check if the 1245 exist int the url -

String extractString(String url, String key){

   if( url.contains(key) ){
      return key;
   }

  return null;
}  

Now for url1 call extractString() method like this -

String url1 = "http://m.facebook.com/messages/1245/abc"
String key = "1234";
String returnValue = extractString(url1, key);
Razib
  • 10,965
  • 11
  • 53
  • 80