1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxStringTokenizer
4 // Author: wxWidgets team
5 // Licence: wxWindows licence
6 /////////////////////////////////////////////////////////////////////////////
9 The behaviour of wxStringTokenizer is governed by the
10 wxStringTokenizer::wxStringTokenizer() or wxStringTokenizer::SetString()
11 with the parameter @e mode, which may be one of the following:
13 enum wxStringTokenizerMode
15 wxTOKEN_INVALID
= -1, ///< Invalid tokenizer mode.
18 Default behaviour: wxStringTokenizer will behave in the same way as
19 @c strtok() (::wxTOKEN_STRTOK) if the delimiters string only contains
20 white space characters but, unlike the standard function, it will
21 behave like ::wxTOKEN_RET_EMPTY, returning empty tokens if this is not
22 the case. This is helpful for parsing strictly formatted data where
23 the number of fields is fixed but some of them may be empty (i.e.
24 @c TAB or comma delimited text files).
29 In this mode, the empty tokens in the middle of the string will be returned,
30 i.e. @c "a::b:" will be tokenized in three tokens @c 'a', @c '' and @c 'b'.
31 Notice that all trailing delimiters are ignored in this mode, not just the last one,
32 i.e. a string @c "a::b::" would still result in the same set of tokens.
37 In this mode, empty trailing tokens (including the one after the last delimiter
38 character) will be returned as well. The string @c "a::b:" will be tokenized in
39 four tokens: the already mentioned ones and another empty one as the last one
40 and a string @c "a::b::" will have five tokens.
42 wxTOKEN_RET_EMPTY_ALL
,
45 In this mode, the delimiter character after the end of the current token (there
46 may be none if this is the last token) is returned appended to the token.
47 Otherwise, it is the same mode as ::wxTOKEN_RET_EMPTY. Notice that there is no
48 mode like this one but behaving like ::wxTOKEN_RET_EMPTY_ALL instead of
49 ::wxTOKEN_RET_EMPTY, use ::wxTOKEN_RET_EMPTY_ALL and
50 wxStringTokenizer::GetLastDelimiter() to emulate it.
55 In this mode the class behaves exactly like the standard @c strtok() function:
56 the empty tokens are never returned.
61 /// Default wxStringTokenizer delimiters are the usual white space characters.
62 #define wxDEFAULT_DELIMITERS " \t\r\n"
65 @class wxStringTokenizer
67 wxStringTokenizer helps you to break a string up into a number of tokens.
68 It replaces the standard C function @c strtok() and also extends it in a
71 To use this class, you should create a wxStringTokenizer object, give it the
72 string to tokenize and also the delimiters which separate tokens in the string
73 (by default, white space characters will be used).
75 Then wxStringTokenizer::GetNextToken() may be called repeatedly until
76 wxStringTokenizer::HasMoreTokens() returns @false.
81 wxStringTokenizer tokenizer("first:second:third:fourth", ":");
82 while ( tokenizer.HasMoreTokens() )
84 wxString token = tokenizer.GetNextToken();
93 @see ::wxStringTokenize()
95 class wxStringTokenizer
: public wxObject
99 Default constructor. You must call SetString() before calling any other
104 Constructor. Pass the string to tokenize, a string containing
105 delimiters, and the @a mode specifying how the string should be
110 wxStringTokenizer(const wxString
& str
,
111 const wxString
& delims
= wxDEFAULT_DELIMITERS
,
112 wxStringTokenizerMode mode
= wxTOKEN_DEFAULT
);
115 Returns the number of tokens remaining in the input string. The number
116 of tokens returned by this function is decremented each time
117 GetNextToken() is called and when it reaches 0, HasMoreTokens()
120 size_t CountTokens() const;
123 Returns the delimiter which ended scan for the last token returned by
124 GetNextToken() or @c NUL if there had been no calls to this function
125 yet or if it returned the trailing empty token in
126 ::wxTOKEN_RET_EMPTY_ALL mode.
130 wxChar
GetLastDelimiter() const;
133 Returns the next token or empty string if the end of string was reached.
135 wxString
GetNextToken();
138 Returns the current position (i.e.\ one index after the last returned
139 token or 0 if GetNextToken() has never been called) in the original
142 size_t GetPosition() const;
145 Returns the part of the starting string without all token already extracted.
147 wxString
GetString() const;
150 Returns @true if the tokenizer has further tokens, @false if none are left.
152 bool HasMoreTokens() const;
155 Initializes the tokenizer. Pass the string to tokenize, a string
156 containing delimiters, and the @a mode specifying how the string
159 void SetString(const wxString
& str
,
160 const wxString
& delims
= wxDEFAULT_DELIMITERS
,
161 wxStringTokenizerMode mode
= wxTOKEN_DEFAULT
);
165 /** @addtogroup group_funcmacro_string */
169 This is a convenience function wrapping wxStringTokenizer which simply
170 returns all tokens found in the given @a str as an array.
172 Please see wxStringTokenizer::wxStringTokenizer for the description
173 of the other parameters.
175 @return The array with the parsed tokens.
177 @header{wx/tokenzr.h}
180 wxStringTokenize(const wxString
& str
,
181 const wxString
& delims
= wxDEFAULT_DELIMITERS
,
182 wxStringTokenizerMode mode
= wxTOKEN_DEFAULT
);