0

I have a string test in which I can see VD1 and and VD2. How can I extract the value of VD1 and VD2 and store it in string.

String test =
  "DomainName=xyz.zzz.com
   &ModifiedOn=03%2f17%2f2015
   &VD1=MTMwMDE3MDQ%3d
   &VD2=B67E48F6969E99A0BC2BEE0E240D2B5C
   &SiteLanguage=English"

Here value of VD1=MTMwMDE3MDQ%3d and VD2=B67E48F6969E99A0BC2BEE0E240D2B5C. But these are the dynamic values. Here VD1 and VD2 are seperated by '&'.

Reporter
  • 3,897
  • 5
  • 33
  • 47
Little bird
  • 1,106
  • 7
  • 28
  • 58
  • Looks like it is URL encoded, is it from an URL? – Alexandre Lavoie Mar 17 '15 at 13:29
  • Possible duplicate of: [http://stackoverflow.com/questions/13592236/parse-the-uri-string-into-name-value-collection-in-java/13592324#13592324](http://stackoverflow.com/questions/13592236/parse-the-uri-string-into-name-value-collection-in-java/13592324#13592324) – Kevin Cruijssen Mar 17 '15 at 13:34

5 Answers5

1

Try regex like this :

public static void main(String[] args) throws Exception {
    String test = "DomainName=xyz.zzz.com&ModifiedOn=03%2f17%2f2015&VD1=MTMwMDE3MDQ%3d&VD2=B67E48F6969E99A0BC2BEE0E240D2B5C&SiteLanguage=English";
    Pattern p = Pattern.compile("VD1=(.*)&VD2=(.*)&");
    Matcher m = p.matcher(test);
    while(m.find()){
        System.out.println(m.group(1));
        System.out.println(m.group(2));
    }

}

O/P :

MTMwMDE3MDQ%3d
B67E48F6969E99A0BC2BEE0E240D2B5C
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
0

You can use regular expressions or use the String index() and split() methods.

A regular expression that matches and captures the VD1 value is

/VD1=([^&]*)/
neuhaus
  • 3,886
  • 1
  • 10
  • 27
0

If you're sure that theres always a "&" behind the values of VD1 and VD2, this kind of splitting will do:

    String test = "DomainName=xyz.zzz.com&ModifiedOn=03%2f17%2f2015&VD1=MTMwMDE3MDQ%3d&VD2=B67E48F6969E99A0BC2BEE0E240D2B5C&SiteLanguage=English";
    String vd1 = test.substring(test.indexOf("VD1=")+4, test.indexOf("&", test.indexOf("VD1")));
    String vd2 = test.substring(test.indexOf("VD2=")+4, test.indexOf("&", test.indexOf("VD2")));

    System.out.println("VD1:" + vd1 + "\nVD2:" + vd2);

This is only a demo, for production you'd have to extract the indexes for better performance.

fiffy
  • 840
  • 9
  • 16
0

You can use String.split(...) to split a String in pieces. For example, test.split("&") first splits the String in individual tokens (of the form "key=value").

You could do the following to achieve this:

String vd1 = null, vd2 = null;
for (String token : test.split("&")) {
    // For each token, we check if it is one of the keys we need:
    if (token.startsWith("VD1=")) {
        // The number 4 represents the length of the String "vd1="
        vd1 = token.substring(4);
    } else if (token.startsWith("VD2=") {
        vd2 = token.substring(4);
    }
}
System.out.println("VD1 = " + vd1);
System.out.println("VD2 = " + vd2);

However, if you want to parse arbitrary keys, consider using a more robust solution (instead of hard-coding the keys in the for-loop).

Also see the documentation for the String class

Chronio
  • 757
  • 3
  • 8
0
String test = "DomainName=xyz.zzz.com&Test&ModifiedOn=03%2f17%2f2015&VD1=MTMwMDE3MDQ%3d&VD2=B67E48F6969E99A0BC2BEE0E240D2B5C&SiteLanguage=English";
HashMap<String, String> paramsMap  = new HashMap<String, String>();
String[] params = test.split("&");
for (int i=0; i<params.length; i++) {
    String[] param = params[i].split("=");
    String paramName = URLDecoder.decode(param[0], "UTF-8");
    String paramValue = null;
    if(param.length > 1)
        paramValue = URLDecoder.decode(param[1], "UTF-8");

    paramsMap.put(paramName, paramValue);
}
String vd1 = paramsMap.get("VD1");
String vd2 = paramsMap.get("VD2");
Vasily Komarov
  • 1,405
  • 9
  • 11