Split string using substring

I have the following string, let’s call it myString:

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

How can I extract the substrings android.txt and it contains android data?

I tried myString.substring(myString.lastIndexOf('=')+1, myString.length() ); but it returns it contains android data.



1 Comment

  1. Digin Dominic

    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


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.