0

I have string variable String temp="acc=101&name=test"; and now how to get the value of name param from temp string.

ACP
  • 34,682
  • 100
  • 231
  • 371
  • you can make use of split function – ankit Aug 27 '13 at 07:13
  • String temp = "acc=101&name=test"; String[] tempParts = temp.split("name="); System.out.println(tempParts[1]); – ankit Aug 27 '13 at 07:17
  • 2
    If you are in a web environment the classes of your framework probably provide service methods to analyze URL parameters, e.g. method [getParameter of class ServletRequest](http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getParameter(java.lang.String)) – oddparity Aug 27 '13 at 07:25
  • Seems like a duplicate of http://stackoverflow.com/questions/1667278/parsing-query-strings-in-java – Jared Sohn Aug 27 '13 at 07:56

9 Answers9

2
temp.split("&")[1].split("=")[1]
Paul Nikonowicz
  • 3,883
  • 21
  • 39
1
    public static Map<String, String> getParamMap(String query)  
    {  
         String[] params = query.split("&");  
         Map<String, String> map = new HashMap<String, String>();  
         for (String param : params)  
         {  
            String name = param.split("=")[0];  
            String value = param.split("=")[1];  
            map.put(name, value);  
         }  
          return map;  
     }


    String temp="acc=101&name=test";

    Map<String, String> map = getParamMap(temp);

    for(Object object :map.keySet()){
        System.out.println("key= "+object +"  value= "+map.get(object));
    }


   System.out.println(map.get("name"));
Prabhakaran Ramaswamy
  • 25,706
  • 10
  • 57
  • 64
1

Here is a non-general way

String str = "name=";
System.out.println(temp.substring(temp.indexOf(str) + str.length()));

It could be implemented in more general way of course:

String temp = "acc=101&name=test";
StringTokenizer st = new StringTokenizer(temp, "&");
String paramName = "name";
String paramValue = "";
while(st.hasMoreElements()) {
    String str = st.nextToken();
    if (str.contains(paramName)) {
        paramValue = str.substring(str.indexOf(paramName) + paramName.length() + 1);
        break;
    }
}
System.out.println(paramValue);
Ean V
  • 5,091
  • 5
  • 31
  • 39
0
    String temp="acc=101&name=test";
    String[] split = temp.split("&");
    String[] name = split[1].split("=");
    System.out.println(name[1]); 
gjman2
  • 912
  • 17
  • 28
0

You can use a method like below

public static String getValue(String queyStr, String paraamName){
    String[] queries=queyStr.split("&");
    for(String param:queries){
        if(param.indexOf(paraamName)!=-1)
            return param.split("=")[1];
    }
    return null;
}

And call the method like

getValue(temp, "name")
Krushna
  • 5,059
  • 5
  • 32
  • 49
0

Assuming you have constant format :

 String temp="acc=101&name=test";
 String result =temp.substring(temp.lastIndexOf("=")+1,temp.length());

//result is test
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • @downvoter,I would like to see your comment.Thanks for your valuable comment.Sure,it helps me in future to correct my codes :) – Suresh Atta Aug 27 '13 at 07:30
0
   public static void main(String[] args) 
    {
    String temp = "acc=101&name=test";
    System.out.println(temp.split("&")[1].split("=")[1]);
    }
ankit
  • 4,919
  • 7
  • 38
  • 63
0

If you are looking for a way to parse GET-Parameters out of an URL:

public static Map<String, String> splitQuery(URL url) throws UnsupportedEncodingException {
    Map<String, String> query_pairs = new LinkedHashMap<String, String>();
    String query = url.getQuery();
    String[] pairs = query.split("&");
    for (String pair : pairs) {
        int idx = pair.indexOf("=");
        query_pairs.put(URLDecoder.decode(pair.substring(0, idx), "UTF-8"), URLDecoder.decode(pair.substring(idx + 1), "UTF-8"));
    }
    return query_pairs;
}

You can access the returned Map using <map>.get("name"), with the URL given in your question this would return "test".

Khinsu
  • 1,487
  • 11
  • 27
0

I would put the whole parameter in a HashMap so it is easy to get the values.

HashMap<String, String> valuemap = new HashMap<String, String>();

If you do it like so, you have to split the values at the right place...

String temp="acc=101&name=test";

valuemap.put(temp.split("&")[0].split("=")[0], temp.split("&")[0].split("=")[1]);
valuemap.put(temp.split("&")[1].split("=")[0], temp.split("&")[1].split("=")[1]);

...and put them into your HashMap. Than you have a nice collection of all your values and it is also better if you have more than only that two values. If you want the value back, use:

valuemap.get("acc")
valuemap.get("name")
Gerret
  • 2,948
  • 4
  • 18
  • 28