docs/Writerside/topics/string.h.md

changeset 1226
129ef9bb1477
parent 1222
952e712df557
equal deleted inserted replaced
1225:086e63c8dd06 1226:129ef9bb1477
250 bool cx_strtok_next(CxStrtokCtx *ctx, cxstring *token); 250 bool cx_strtok_next(CxStrtokCtx *ctx, cxstring *token);
251 251
252 bool cx_strtok_next_m(CxStrtokCtx *ctx, cxmutstr *token); 252 bool cx_strtok_next_m(CxStrtokCtx *ctx, cxmutstr *token);
253 ``` 253 ```
254 254
255 > Documentation work in progress. 255 You can tokenize a string by creating a _tokenization_ context with `cx_strtok()`,
256 >{style="warning"} 256 and calling `cx_strtok_next()` or `cx_strtok_next_m()` as long as they return `true`.
257
258 The tokenization context is initialized with the string `str` to tokenize,
259 one delimiter `delim`, and a `limit` for the maximum number of tokens.
260 When `limit` is reached, the remaining part of `str` is returned as one single token.
261
262 You can add additional delimiters to the context by calling `cx_strtok_delim()`, and
263 specifying an array of delimiters to use.
264
265 > Regardless of how the context was initialized, you can use either `cx_strtok_next()`
266 > or `cx_strtok_next_m()` to retrieve the tokens. However, keep in mind that modifying
267 > characters in a token returned by `cx_strtok_next_m()` has only defined behavior, when the
268 > underlying `str` is a `cxmutstr`.
269
270 ### Example
271
272 ```C
273 #include <cx/string.h>
274
275 cxstring str = cx_str("an,arbitrarily;||separated;string");
276
277 // create the context
278 CxStrtokCtx ctx = cx_strtok(str, CX_STR(","), 10);
279
280 // add two more delimters
281 cxstring delim_more[2] = {CX_STR("||"), CX_STR(";")};
282 cx_strtok_delim(&ctx, delim_more, 2);
283
284 // iterate over the tokens
285 cxstring tok;
286 while(cx_strtok_next(&ctx, &tok)) {
287 // to something with the tokens
288 // be aware that tok is NOT zero-terminated!
289 }
290 ```
257 291
258 ## Conversion to Numbers 292 ## Conversion to Numbers
259 293
260 For each integer type, as well as `float` and `double`, there are functions to convert a UCX string to a number of that type. 294 For each integer type, as well as `float` and `double`, there are functions to convert a UCX string to a number of that type.
261 295

mercurial