0

Hi i would like to extract a string from a sentence : DocumentId=38416184&Viewer=D&ContentFormat=pdf

here am trying to extract only the string present after "="

the expected output is:

38416184
D
pdf

I tried using split () method

String[] tokens = sCurrentLine.split("=|&",-1);

here i got an output as :

38416184
Viewer
D
ContentFormat
pdf 

please do needful.Thanks in advance :)

user3141052
  • 31
  • 1
  • 3
  • 6
  • possible duplicate of [How to extract parameters from a given url](http://stackoverflow.com/questions/5902090/how-to-extract-parameters-from-a-given-url) – JeffC Aug 27 '15 at 14:01

7 Answers7

1

Do it with two splits. First split the string by the & character to get the key-value pairs and then iterate over this pairs and split by = character. Always the secound token is the value you want to get.

    String sCurrentLine = "DocumentId=38416184&Viewer=D&ContentFormat=pdf";
    for(String pair : sCurrentLine.split("&"))
    {
        String value = pair.split("=")[1];
        System.out.println(value);
    }
kai
  • 6,702
  • 22
  • 38
0

First you could split it by "&" - so you will get a String array, then you could iterate on it and split again each String by "=".

String str="DocumentId=38416184&Viewer=D&ContentFormat=pdf".
String[] first=str.split("&");
for(int i=0;i<first.length;i++) {
   String[] then=first[i].split("=");
   String toGet=then[1];
   System.out.println(toGet);
}
Leah
  • 458
  • 3
  • 15
0
String[] tokens = sCurrentLine.split("&");
for(String token : tokens){
   System.println(token.split("=")[1]);
}
Fran Montero
  • 1,679
  • 12
  • 24
0
String[] params = sCurrentLine.split("&");

List<String> paramValues = new ArrayList<String>();
for (String p : params) {
    String pVal = p.split("=")[1];
    paramValues.add(pVal);
}

// paramValue contains the desired output

Raman Shrivastava
  • 2,923
  • 15
  • 26
0

So far, the other answers use split() for both the & and the =. Using split("=") is error prone if data is bad. Use indexOf('=') instead.

String sCurrentLine = "DocumentId=38416184&Viewer=D&ContentFormat=pdf&NoValue&Complex=a=b";
for (String param : sCurrentLine.split("&")) {
    int idx = param.indexOf('=');
    System.out.println(idx == -1 ? null : param.substring(idx + 1));
}

With the added bad data above, output is:

38416184
D
pdf
null
a=b
Andreas
  • 154,647
  • 11
  • 152
  • 247
0

There's already an Apache library to do this

import java.net.URI;
import java.net.URISyntaxException;
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
...
String url = "https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=java%20extract%20url%20parameters";
List<NameValuePair> params = URLEncodedUtils.parse(new URI(url), "UTF-8");
for (NameValuePair param : params)
{
    System.out.println(param.getName() + " : " + param.getValue());
}
JeffC
  • 22,180
  • 5
  • 32
  • 55
-1

All is pretty easy:

  1. split to different parts by "&"

  2. get second part after "="

 String[] words= str.split("&");

 foreach (var word in words)
 {
     word = word.split("=")[1]
 }

and words array after this must contain all needed values.

Andrew_STOP_RU_WAR_IN_UA
  • 9,318
  • 5
  • 65
  • 101