-6

I am new to android and i have a following scenario below.

I have the following string below

file=android.txt&data="sampleExample"

How can I extract the substrings android.txt and sampleExample?

NOTE: I tried the following code

myString.substring(myString.lastIndexOf('=')+1, myString.length() );

but it returns output as SampleExample.

Please help me on this issue

Thanks

likith sai
  • 527
  • 1
  • 6
  • 21
  • 1
    Does it have to me `substring`? – Pshemo May 14 '15 at 15:46
  • 3
    What have you tried? Check out [the API](https://docs.oracle.com/javase/8/docs/api/) for useful functions in the String class. – Kevin Workman May 14 '15 at 15:47
  • This question has been answered here: http://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java – Mugambo May 14 '15 at 15:49
  • are you saying you want to take as input "file=android.txt&data=it contains android data" and then with a call to string.split have just "android.txt" and "it contains android data" ?? – shaunvxc May 14 '15 at 15:49
  • Your second comment negates first one. I asked if you have to use `substring` and you said yes (at least I assume that it was answer for my comment), but then Mugambo asks you if you "*with a call to string.`split`*" and you are saying yes... So which is it, do you have to use `substring` or are you allowed to use other methods like `split`? – Pshemo May 14 '15 at 15:53
  • you also said you DO want to get "it contains android data" – shaunvxc May 14 '15 at 15:54

4 Answers4

2

You can simply use split method from String class.

Split file=android.txt&data=it contains android data

on & to get array of "file=android.txt" and "data=it contains android data".

Then you can split each of this elements on = so

file=android.txt -> file, android.txt
data=it contains android data -> data, it contains android data

So your code can look like

foreach element in data.split(&)
   array = element.split(=)
   get second element from array
Pshemo
  • 122,468
  • 25
  • 185
  • 269
2

Here is what you're looking for:

String myString = "file=android.txt&data=it contains android data";

Pattern pattern = Pattern.compile("^file=([^&]+)&data=(.+)");
Matcher matcher = pattern.matcher(myString);
if (matcher.find()) {
    String first = matcher.group(1);
    String second = matcher.group(2);
}

Tried and tested.

Karim
  • 5,298
  • 3
  • 29
  • 35
0
String body = "file=android.txt&data=it contains android data"
String[] data = body.substring(body.indexOf('=')+1, body.length()).split("&data=");

// data[0] = "android.txt"
// data[1] = "it contains android data"
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
-1

Your input string looks like a query string. I. e. a list of key/value pairs that are seperated by '&'.

I would split each key/value pair into a String and then extract the value after the first '='.

    String input = "file=android.txt&data=it contains android data"; 
    String[] items = input.split("&"); 
    String[] results = new String[items.length]; 
    for (int i = 0; i < items.length; i++)
    {
        int pos = item.indexOf("=");
        if (pos == -1)
            results[i] = null; 
        else
            results[i] = items[i].substring(pos + 1); 
    }

    // results now holds your strings. 
Murkhog
  • 1
  • 1