Consider the following scenario with two different strings:
Row1: MyID-MyName-MyAddress-MyNumber-MyNumber2-MyAlias
Row2: MyID-MyName-MyAddress-MyNumber--MyAlias
In the second example, the value for MyNumber2
is missing. I need to extract each attribute using strtok()
. So far, this is my code:
MyID = strtok (str, "-"); //where 'str' is the full string
MyName = strtok (NULL, "-");
MyAddress = strtok (NULL, "-");
MyNumber = strtok (NULL, "-");
MyNumber2 = strtok (NULL, "-");
MyAlias = strtok (NULL, "-");
The first example works good, and I am able to store each attribute inside the variables. However, in the second example, I am having troubles:
When getting into the variable MyNumber2
, strtok()
does not return an empty string (as I would like). Instead, it reads through the string until the first character that does not match the delimiter "-"
, thus ignoring the existence of the empty value in the string.
Is there any possibility to split the string just once per delimiter?