masmic
masmic

Reputation: 3564

Split string point delimited

I'm developing an app for M2M communication via SMS between the mobile phone and an electronic device which has a SIM card.

When I send a command from the mobile to the device, this device returns a confirmation string to the mobile which has this structure.

Confirmation OK:

DEV33456, 15, Date: yy/mm/dd-hh:mm:ss. OK

Confirmation ERROR:

DEV33456, 15, Date: yy/mm/dd-hh:mm:ss. ERROR

Now in my app I have to manage this message to get the relevant information. For example, the first part of the message is the identify code (DEV33456) so to get it I split the message:

String[] separated = message.split(",");
String ident = separated[0]; //DEV33456

But now, I need to get the last word (OK or ERROR) to determine if the message is OK or not. To do that I thought that the easiest way should be to split the message using the point before the OK or ERROR word. Is the only point in the entire message, so it should split it like this:

String[] separated = message.split(".");
String unused = separated[0]; //DEV33456, 15, Date: yy/mm/dd-hh:mm:ss
String error = separated[1]; //OK or ERROR

But it is throwing an ArrayIndexOutOfBoundsException and debuging it I see that after the first line where the separated string array should have a lenght of 2, it has a lenght of 0.

Is there any problem on spliting the string using a "."? I have done the same procedure to split it using a "," and it has done int correctly.

Upvotes: 0

Views: 79

Answers (3)

luiscosta
luiscosta

Reputation: 855

Check if this is want you want:

if(message.substring(message.length()-2, message.length())
         .toLowerCase().contains("ok"))
    Log.d("yes");
else
    Log.d("no");

Hopefully it is.

Upvotes: 0

Andreas Petersson
Andreas Petersson

Reputation: 16518

why not just use

message.endsWith("OK")

Upvotes: 2

Tim B
Tim B

Reputation: 41188

Java split uses regular expressions, . is a special character in regular expressions.

You need to escape it by using the string "\\.".

Upvotes: 7

Related Questions