0

It's possible to extract a specific item in String with split function

example:

offers/BESTOFFERS/FRTNE/FRPAR/2015-01-05?passengers=STANDARD:1&returnDate=2015-01-12&maxVia=0&withThac=false

i want to extract just returnDate

ouptut why i want:

2015-01-12

OR

i want to extract just passengers

ouptut why i want:

STANDARD:1

Mercer
  • 9,736
  • 30
  • 105
  • 170
  • 2
    http://stackoverflow.com/a/22191247/1927832 might help – Suresh Atta Jan 06 '15 at 11:10
  • 4
    SO is not to write code for you.. Show us what you have tried, where you failed. what error you got. – mtk Jan 06 '15 at 11:12
  • possible duplicate of [How to extract parameters from a given url](http://stackoverflow.com/questions/5902090/how-to-extract-parameters-from-a-given-url) – RealSkeptic Jan 06 '15 at 11:13

3 Answers3

1

If you really need to stick on the split method you could solve it for example like this

String str = "offers/BESTOFFERS/FRTNE/FRPAR/2015-01-05?passengers=STANDARD:1&returnDate=2015-01-12&maxVia=0&withThac=false";
int paramDelim = str.indexOf('?');
String parmeters = str.substring(paramDelim + 1, str.length());
String[] parts = parmeters.split("[&=]");
System.out.println("parts = " + Arrays.toString(parts));

parts contain the paramer names (odd entries) and the values (even entries).

If you don't need to stick on the split method try one of the proposed URL parser solutions.

SubOptimal
  • 22,518
  • 3
  • 53
  • 69
0

You can also try the below approach of using HashMap

void populateMap()
{
    Map<String, String> myMap = new HashMap<String, String>();
    String uri = "offers/BESTOFFERS/FRTNE/FRPAR/2015-01-05?passengers=STANDARD:1&returnDate=2015-01-12&maxVia=0&withThac=false";
    int len = uri.indexOf('?');
    String input = str.substring(len + 1, uri.length());
    for(String retVal : input.split("&")
    {
        String[] innerRet = retVal.split(":");
        myMap.put(innerRet[0],innerRet[1]);
    }
}

String retValue (String key)
{
    return myMap.get(key);
}
Kamal Kishore
  • 61
  • 1
  • 5
-1
    String str = "offers/BESTOFFERS/FRTNE/FRPAR/2015-01-05?passengers=STANDARD:1&returnDate=2015-01-12&maxVia=0&withThac=false";

    String returnDate = str.split("&")[1].replaceAll
    ("returnDate=","").trim();

    String passengers= str.split("?")[1].split("&")[0].replaceAll
    ("passengers=","").trim();
Manjunath D R
  • 371
  • 2
  • 8