Husnain Aslam
Husnain Aslam

Reputation: 865

Android: Reading the string to specific character

I am reading commands from micro controller in this format @transfer:Off# but some times the micro controller sends garbage commands like @transfer:Off#@waste# as the controller's programming is done by some other unknown programmer. I only need @transfer:Off# string which starts from @ and ends with first # The compiler should wait for @ at the start and read the string until first # comes and stop reading on wards.

I am getting the normal commands by using following chunk of code.

if(values.length > -1){
    command = values[0].substring(1, values[0].length() - 1);
    int commandLength = command.split(":").length;
    if(commandLength>1){
        identifier = command.split(":")[0];
        value = command.split(":")[1];
    }else{
        value = "No";
    }
 }

How I get this done?

Upvotes: 1

Views: 91

Answers (1)

visionix visionix
visionix visionix

Reputation: 780

if you don't know the length of the string that the micro controller is sending you, you will need to split the string by # and getting the first member of the array.

String recieveString = "@transfer:Off#@waste#";
String resStr = recieveString.split("#")[0]; 

Upvotes: 1

Related Questions