javaandroidadt

Split string using substring


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


Solution

  • 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