I want to split this:
char* value = "12;32;blue"; or string value = "12;32;blue";
into this vars:
TV = 12; AR = 32; LED = "blue";
is it possible?
Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It only takes a minute to sign up.Sign up to join this community
For C-strings (
char*), your best option (in terms of performance and memory consumption) is to use
char* value = "12;32;blue"; char* token = strtok(value, ";"); int TV = atoi(token); token= strtok(0, ";"); int AR = atoi(token); token = strtok(0, ";"); char* LED = token;
Note 1: the code above takes it for granted that
value is properly formatted, i.e. contains 3 parts split by
;. If it is not sure, then you should add additional checks on
token value returned by
strtok is modifiying its
value argument, so that at the end of the code above,
value will not be equal to
12;32;blue any longer.
LED variable above will point directly to the character
value, which means that if
value is modified afterwards,
LED might be modified as well.