C++ String Tokenizer equivalent?

Apr 6, 2011 at 9:03pm
I recently fell in love with the String Tokenizer in Java while working on an assignment for my Java course. I was just wondering if there are any equivalents of the String Tokenizer in C++? If so, where can I find the documentation?
Apr 6, 2011 at 9:06pm
There is no inbuilt tokenizing functionality (that I am aware of), but it's not hard to implement it yourself - what functionality do you need (I am not very fluent with java, but the actual tokenizing of strings can be done with a few lines of code)?
Apr 6, 2011 at 9:15pm
You can look at the boost.tokenizer library:

http://www.boost.org/doc/libs/1_46_1/libs/tokenizer/index.html

Apr 6, 2011 at 9:16pm
I don't know the Java tokenizer, but the C standard library (which is part of C++) has strtok(): http://www.cplusplus.com/reference/clibrary/cstring/strtok/

If you want a tokenizer which also categorizes tokens, you'll have to write one. But it's pretty easy to do.
Apr 7, 2011 at 1:04am
Yeah, it's very easy, almost trivial. e.g.

#define UNKNOWNCHAR _T('_')
#define ISTOKEN(x) (iswalpha(x) || (x == UNKNOWNCHAR))

/-----------------------------------------------------------------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
int ReturnToken(LPTSTR str, int i, LPTSTR token)
{
	int j=0;

	token[0] = 0;
	while(ISTOKEN(str[i]))
	{
		token[j++] = str[i++];
		token[j] = 0;
	}
	if(str[i] == 0) return -1;
	return j;
}


Just an example from something I wrote. But you can tailor ISTOKEN to include any tokens you like. Please excuse the Visual C++ syntax.
Last edited on Apr 7, 2011 at 1:05am
Apr 7, 2011 at 1:23am
filipe, thanks. I'll make sure to read that.
Topic archived. No new replies allowed.