user3972619
user3972619

Reputation:

How to get in String from String Array by Split in Java Android

This is my Result where i got response from server that I want to get by Soap.I can parse this value by JSON but I'm having a problem doing so as I wish to get this value split.

Result=1~Saved successfully~{ "TABLE":[{ "ROW":[ { "COL":{ "UserID":"30068"}} ]}]}

I am using this code to get UserId values in tmpVal, however am not obtaining my desired results.

String tmpVal = returnValue.toString().split("~")[3];

Upvotes: 0

Views: 133

Answers (2)

Juxhin
Juxhin

Reputation: 5620

As stated above in the comment section, Arrays start with index 0, thus if you have an array with 3 elements, the index are 0..1..2 and not 1..2..3

All you have to do is change the String tmpVal = returnValue.toString().split("~")[3]; to:

String tmpVal = returnValue.toString().split("~")[2];

As that will obtain the 3rd element instead of the fourth element as you've been trying to do.

You may also check out this question

Upvotes: 0

Eran
Eran

Reputation: 393896

String tmpVal = returnValue.toString().split("~")[3];

This would give you the 4th String in the array produced by split, but since split only produced an array of 3 Strings, this code gives you an exception.

If you want to get the last part of the split response - { "TABLE":[{ "ROW":[ { "COL":{ "UserID":"30068"}} ]}]} - you need returnValue.toString().split("~")[2].

Of course, it would be safer to first test how many Strings were returned by split :

String[] splitResult = returnValue.toString().split("~");
if (splitResult.length > 2) {
    tempVal = splitResult[2];
}

Upvotes: 3

Related Questions