Module: Essential Tools Module Group: String Processing Classes
Does not inherit
#include <rw/ctoken.h> RWCString str("a string of tokens"); RWCTokenizer(str); // Lex the above string
Class RWCTokenizer is designed to break a string up into separate tokens, delimited by an arbitrary white space. Think of it as an iterator for strings and as an alternative to the ANSI C function strtok() which has the unfortunate side effect of changing the tokenized string.
None
#include <iostream> #include <rw/ctoken.h> int main () { RWCString a ("Something is rotten in the state of " "Denmark and Hamlet is taking out the trash."); RWCTokenizer next(a); // Tokenize the string a RWCString token; // Will receive each token // Advance until the null string is returned: while (!(token=next()).isNull()) std::cout << token << "\n"; }
Program output:
Something is rotten in the state of Denmark and Hamlet is taking out the trash.
RWCTokenizer(const RWCString& s);
Constructs a tokenizer to lex the string s.
RWCSubString operator()();
Advances to the next token and return it as a substring. The tokens are delimited by any of the four characters in " \t\n\0" (space, tab, newline and null).
RWCSubString operator()(const char* s);
Advances to the next token and return it as a substring. The tokens are delimited by any character in s, or any embedded null.
RWCSubString operator()(const char* s,size_t num);
Advances to the next token and return it as a substring. The tokens are delimited by any of the first num characters in s. Buffer s may contain nulls, and must contain at least num characters. Tokens will not be delimited by nulls unless s contains nulls.
RWCString operator()(RWTRegex<char>& regex);
Returns the next token using a delimiter pattern represented by the regular expression pattern regex.
This method, unlike the other operator() overloads, allows a single occurrence of a delimiter to span multiple characters.
For example, consider the RWCTokenizer instance tok. The statement tok(RWCString("ab")) treats either a or b as a delimiter character. On the other hand, tok(RWTRegex<char>("ab")) treats the two-character pattern, ab, as a single delimiter.
This method consumes consecutive occurrences of delimiters and skips over any empty fields that may be present in the string. To obtain empty fields as well as non-empty fields, use the nextToken() method.
boolean done() const
Returns true if the last token from the search string has been extracted. If the last token has not been extracted, it will return false. When using the function call operator interface, this is the same as the last non-empty token having been returned.
RWCString nextToken()
Returns the next token using a default set of delimiter characters.
This method may return an empty token if there are consecutive occurrences of any delimiter character in the search string.
RWCString nextToken(const RWCString& str)
Returns the next token using a specified string of delimiter characters.
This method may return an empty token if there are consecutive occurrences of any delimiter character in the search string.
RWCString nextToken(const RWCString& str, size_t num);
Returns the next token using the first num code units from the given string str of delimiter characters.
This method may return an empty token if there are consecutive occurrences of any delimiter character in the search string.
RWCString nextToken(TRegex<char>& regex);
Returns the next token using a delimiter pattern represented by a regular expression pattern.
Unlike the other nextToken() overloads, this method allows a single occurrence of a delimiter to span multiple characters.
For example, nextToken(RWCString("ab")) treats either a or b as a delimiter character. Conversely, nextToken(RWTRegex<char>("ab")) treats the two-character pattern ab as a single delimiter.
This method may return an empty token if there are consecutive occurrences of any delimiter character in the search string.
© Copyright Rogue Wave Software, Inc. All Rights Reserved.
Rogue Wave and SourcePro are registered trademarks of Rogue Wave Software, Inc. in the United States and other countries. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.