]> git.saurik.com Git - wxWidgets.git/blob - docs/latex/wx/tokenizr.tex
documented wxStringTokenize()
[wxWidgets.git] / docs / latex / wx / tokenizr.tex
1 \section{\class{wxStringTokenizer}}\label{wxstringtokenizer}
2
3 wxStringTokenizer helps you to break a string up into a number of tokens. It
4 replaces the standard C function {\tt strtok()} and also extends it in a
5 number of ways.
6
7 To use this class, you should create a wxStringTokenizer object, give it the
8 string to tokenize and also the delimiters which separate tokens in the string
9 (by default, white space characters will be used).
10
11 Then \helpref{GetNextToken}{wxstringtokenizergetnexttoken} may be called
12 repeatedly until it \helpref{HasMoreTokens}{wxstringtokenizerhasmoretokens}
13 returns \false.
14
15 For example:
16
17 \begin{verbatim}
18
19 wxStringTokenizer tkz(wxT("first:second:third:fourth"), wxT(":"));
20 while ( tkz.HasMoreTokens() )
21 {
22 wxString token = tkz.GetNextToken();
23
24 // process token here
25 }
26 \end{verbatim}
27
28 By default, wxStringTokenizer will behave in the same way as {\tt strtok()} if
29 the delimiters string only contains white space characters but, unlike the
30 standard function, it will return empty tokens if this is not the case. This
31 is helpful for parsing strictly formatted data where the number of fields is
32 fixed but some of them may be empty (i.e. {\tt TAB} or comma delimited text
33 files).
34
35 The behaviour is governed by the last
36 \helpref{constructor}{wxstringtokenizerwxstringtokenizer}/\helpref{SetString}{wxstringtokenizersetstring}
37 parameter {\tt mode} which may be one of the following:
38
39 \twocolwidtha{5cm}%
40 \begin{twocollist}\itemsep=0pt
41 \twocolitem{{\tt wxTOKEN\_DEFAULT}}{Default behaviour (as described above):
42 same as {\tt wxTOKEN\_STRTOK} if the delimiter string contains only
43 whitespaces, same as {\tt wxTOKEN\_RET\_EMPTY} otherwise}
44 \twocolitem{{\tt wxTOKEN\_RET\_EMPTY}}{In this mode, the empty tokens in the
45 middle of the string will be returned, i.e. {\tt "a::b:"} will be tokenized in
46 three tokens `a', `' and `b'.}
47 \twocolitem{{\tt wxTOKEN\_RET\_EMPTY\_ALL}}{In this mode, empty trailing token
48 (after the last delimiter character) will be returned as well. The string as
49 above will contain four tokens: the already mentioned ones and another empty
50 one as the last one.}
51 \twocolitem{{\tt wxTOKEN\_RET\_DELIMS}}{In this mode, the delimiter character
52 after the end of the current token (there may be none if this is the last
53 token) is returned appended to the token. Otherwise, it is the same mode as
54 {\tt wxTOKEN\_RET\_EMPTY}.}
55 \twocolitem{{\tt wxTOKEN\_STRTOK}}{In this mode the class behaves exactly like
56 the standard {\tt strtok()} function. The empty tokens are never returned.}
57 \end{twocollist}
58
59 \wxheading{Derived from}
60
61 \helpref{wxObject}{wxobject}
62
63 \wxheading{See also}
64
65 \helpref{wxStringTokenize}{wxstringtokenize}
66
67 \wxheading{Include files}
68
69 <wx/tokenzr.h>
70
71 \latexignore{\rtfignore{\wxheading{Members}}}
72
73
74 \membersection{wxStringTokenizer::wxStringTokenizer}\label{wxstringtokenizerwxstringtokenizer}
75
76 \func{}{wxStringTokenizer}{\void}
77
78 Default constructor. You must call
79 \helpref{SetString}{wxstringtokenizersetstring} before calling any other
80 methods.
81
82 \func{}{wxStringTokenizer}{\param{const wxString\& }{str}, \param{const wxString\& }{delims = " $\backslash$t$\backslash$r$\backslash$n"}, \param{wxStringTokenizerMode }{mode = wxTOKEN\_DEFAULT}}
83
84 Constructor. Pass the string to tokenize, a string containing delimiters
85 and the mode specifying how the string should be tokenized.
86
87
88 \membersection{wxStringTokenizer::CountTokens}\label{wxstringtokenizercounttokens}
89
90 \constfunc{int}{CountTokens}{\void}
91
92 Returns the number of tokens remaining in the input string. The number of
93 tokens returned by this function is decremented each time
94 \helpref{GetNextToken}{wxstringtokenizergetnexttoken} is called and when it
95 reaches $0$ \helpref{HasMoreTokens}{wxstringtokenizerhasmoretokens} returns
96 \false.
97
98
99 \membersection{wxStringTokenizer::HasMoreTokens}\label{wxstringtokenizerhasmoretokens}
100
101 \constfunc{bool}{HasMoreTokens}{\void}
102
103 Returns \true if the tokenizer has further tokens, \false if none are left.
104
105
106 \membersection{wxStringTokenizer::GetNextToken}\label{wxstringtokenizergetnexttoken}
107
108 \func{wxString}{GetNextToken}{\void}
109
110 Returns the next token or empty string if the end of string was reached.
111
112
113 \membersection{wxStringTokenizer::GetPosition}\label{wxstringtokenizergetposition}
114
115 \constfunc{size\_t}{GetPosition}{\void}
116
117 Returns the current position (i.e. one index after the last returned
118 token or 0 if GetNextToken() has never been called) in the original
119 string.
120
121
122 \membersection{wxStringTokenizer::GetString}\label{wxstringtokenizergetstring}
123
124 \constfunc{wxString}{GetString}{\void}
125
126 Returns the part of the starting string without all token already extracted.
127
128
129 \membersection{wxStringTokenizer::SetString}\label{wxstringtokenizersetstring}
130
131 \func{void}{SetString}{\param{const wxString\& }{to\_tokenize}, \param{const wxString\& }{delims = " $\backslash$t$\backslash$r$\backslash$n"}, \param{wxStringTokenizerMode }{mode = wxTOKEN\_DEFAULT}}
132
133 Initializes the tokenizer.
134
135 Pass the string to tokenize, a string containing delimiters,
136 and the mode specifying how the string should be tokenized.
137