10 years, 10 months ago.

Parsing strings with strsep instead of strtok

Hi all,

I want to split a coma delimited string and return a pointer or buffer to a token to that string. If the buffer has less attributes and only a number of delimiters I want it to continue to tokenize the string, not fail and return they empty string.

So I can't use strtok, and I cant use sscanf.

I want to do something like this:

    char test[100] = "GPS,TIME,LATITUDE,LONGITUDE,SATALITE,NORTH,SOUTH,,*6A";
	char *buf = test;
	char **bp = &buf;
	char *tok;
	while (tok = strsep(bp, " ,"))
	{
		if (*tok =='\0')
		{
			printf("Empty string detected \n");
		}
		else
		{	
			printf("tok = \"%s\"\n", tok);
		}
	}

However the basic mbed library does not have strsep ! :-( ...

Anyone had to do this or know of a library that I can easily do this?

Kind regards, Nicholas.

2 Answers

10 years, 10 months ago.

To split a C++ string in tokens, I use the tokenizer from
http://www.oopweb.com/CPP/Documents/CPPHOWTO/Volume/C++Programming-HOWTO-7.html

Robert

Accepted Answer
10 years, 10 months ago.

Thanks Robert.

Just trying out the C++ string and vector classes as my tokenizer. I hope the mbed can handle it! :-)

Kind regards, Nicholas.