1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxStringTokenizer
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
10 @class wxStringTokenizer
13 wxStringTokenizer helps you to break a string up into a number of tokens. It
14 replaces the standard C function @c strtok() and also extends it in a
17 To use this class, you should create a wxStringTokenizer object, give it the
18 string to tokenize and also the delimiters which separate tokens in the string
19 (by default, white space characters will be used).
21 Then wxStringTokenizer::GetNextToken may be called
22 repeatedly until it wxStringTokenizer::HasMoreTokens
28 wxStringTokenizer tkz(wxT("first:second:third:fourth"), wxT(":"));
29 while ( tkz.HasMoreTokens() )
31 wxString token = tkz.GetNextToken();
37 By default, wxStringTokenizer will behave in the same way as @c strtok() if
38 the delimiters string only contains white space characters but, unlike the
39 standard function, it will return empty tokens if this is not the case. This
40 is helpful for parsing strictly formatted data where the number of fields is
41 fixed but some of them may be empty (i.e. @c TAB or comma delimited text
44 The behaviour is governed by the last
45 @ref wxStringTokenizer::wxstringtokenizer
46 constructor/wxStringTokenizer::SetString
47 parameter @c mode which may be one of the following:
54 Default behaviour (as described above):
55 same as @c wxTOKEN_STRTOK if the delimiter string contains only
56 whitespaces, same as @c wxTOKEN_RET_EMPTY otherwise
62 In this mode, the empty tokens in the
63 middle of the string will be returned, i.e. @c "a::b:" will be tokenized in
64 three tokens 'a', '' and 'b'. Notice that all trailing delimiters are ignored
65 in this mode, not just the last one, i.e. a string @c "a::b::" would
66 still result in the same set of tokens.
69 @c wxTOKEN_RET_EMPTY_ALL
72 In this mode, empty trailing tokens
73 (including the one after the last delimiter character) will be returned as
74 well. The string @c "a::b:" will be tokenized in four tokens: the already
75 mentioned ones and another empty one as the last one and a string
76 @c "a::b::" will have five tokens.
82 In this mode, the delimiter character
83 after the end of the current token (there may be none if this is the last
84 token) is returned appended to the token. Otherwise, it is the same mode as
85 @c wxTOKEN_RET_EMPTY. Notice that there is no mode like this one but
86 behaving like @c wxTOKEN_RET_EMPTY_ALL instead of
87 @c wxTOKEN_RET_EMPTY, use @c wxTOKEN_RET_EMPTY_ALL and
88 wxStringTokenizer::GetLastDelimiter to emulate it.
94 In this mode the class behaves exactly like
95 the standard @c strtok() function: the empty tokens are never returned.
102 @see wxStringTokenize()
104 class wxStringTokenizer
: public wxObject
109 Constructor. Pass the string to tokenize, a string containing delimiters
110 and the mode specifying how the string should be tokenized.
113 wxStringTokenizer(const wxString
& str
,
114 const wxString
& delims
= " \t\r\n",
115 wxStringTokenizerMode mode
= wxTOKEN_DEFAULT
);
119 Returns the number of tokens remaining in the input string. The number of
120 tokens returned by this function is decremented each time
121 GetNextToken() is called and when it
122 reaches 0 HasMoreTokens() returns
125 int CountTokens() const;
128 Returns the delimiter which ended scan for the last token returned by
129 GetNextToken() or @c NUL if
130 there had been no calls to this function yet or if it returned the trailing
131 empty token in @c wxTOKEN_RET_EMPTY_ALL mode.
135 wxChar
GetLastDelimiter();
138 Returns the next token or empty string if the end of string was reached.
140 wxString
GetNextToken() const;
143 Returns the current position (i.e. one index after the last returned
144 token or 0 if GetNextToken() has never been called) in the original
147 size_t GetPosition() const;
150 Returns the part of the starting string without all token already extracted.
152 wxString
GetString() const;
155 Returns @true if the tokenizer has further tokens, @false if none are left.
157 bool HasMoreTokens() const;
160 Initializes the tokenizer.
161 Pass the string to tokenize, a string containing delimiters,
162 and the mode specifying how the string should be tokenized.
164 void SetString(const wxString
& to_tokenize
,
165 const wxString
& delims
= " \t\r\n",
166 wxStringTokenizerMode mode
= wxTOKEN_DEFAULT
);