|
|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
" m2 n, I. L9 ~2 v, l( r' d5 r
char * strtok ( char * string, const char * delimiters );) `; m& }1 y* T( B; O2 g
- B3 ]; a; I; J9 I' ?% USequentially truncate string if delimiter is found.. e, [" v( D6 W! X4 C/ V
If string is not NULL, the function scans string for the first occurrence of any character included in delimiters. If it is found, the function overwrites the delimiter in string by a null-character and returns a pointer to the token, i.e. the part of the scanned string previous to the delimiter.
1 v( R9 \2 S% Q. Y2 {% G After a first call to strtok, the function may be called with NULL as string parameter, and it will follow by where the last call to strtok found a delimiter.
4 U' d% \8 e) u7 u delimiters may vary from a call to another.2 t. g. a- C1 l3 F; @& {# u
2 R, x# }- n( t# D. y
Parameters.6 j( ~6 f# x- X( R0 z3 y/ o, y
* }) i, S7 y7 Z1 R$ W4 y9 q" w" q
string
0 _1 L2 o4 P8 C3 d3 E! d& pNull-terminated string to scan.
; U! D: ~2 v7 {# i) e4 oseparator! ^ c* b! ^& x+ _
Null-terminated string containing the separators.+ x! {; R% Y: j3 J" \9 F0 ]
Return Value.
/ A( Y. f% o4 l9 G A pointer to the last token found in string. NULL is returned when there are no more tokens to be found.! ]- a/ P5 ~/ z7 }/ U1 ]$ Y" g
# H. d+ s* F, ]4 cPortability.- G' J$ e! g V
Defined in ANSI-C.
8 K# B3 R; _6 X" A3 z
/ y" n" Z' _. d7 h$ m1 J- vExample.& g0 D) k& H' i. K
$ O7 E8 Y& y+ w. U) ~/* strtok example */# T/ H. x; Q" Q# c+ K9 q
#include <stdio.h>) j3 w% m( Q# C- t8 o
#include <string.h>
6 ?+ Q0 k' y1 c4 O7 G- h- q
/ S% ?$ G4 C$ H. rint main ()
; A" _' t7 j e) ?, p{/ B7 I1 e" [) X
char str[] ="This is a sample string,just testing.";
0 o% O# I5 P6 L g6 n7 |) P4 R char * pch;) E1 k) T C5 Q
printf ("Splitting string \"%s\" in tokens:\n",str);
( l, Y9 f# D5 ]3 E+ q7 c pch = strtok (str," ");
) r3 w) w5 U4 u, M: A# | while (pch != NULL)& Z3 a! [$ j7 Z( \
{, C Z5 [: I+ X S0 t, g% S
printf ("%s\n",pch);$ c2 }4 o7 l* g6 W% J5 `
pch = strtok (NULL, " ,.");0 e8 ?' E( C5 C
}8 m6 N6 a" s0 f5 \9 W
return 0;
7 `# s5 [7 O* q& A/ }" G}" g/ N, L1 p2 ?+ C
Output:
" f2 k+ q& o; fSplitting string "This is a sample string,just testing." in tokens:" ?+ W+ b% k; L/ V+ \7 B
This
4 ]+ X7 H+ i' c Jis; t2 Q8 _2 d0 b! l1 V
a+ g4 G6 ]9 Y# x( V. e3 L% u6 N
sample
' d, Z0 o4 G r. p. I& g# W" `string) Z+ ?! y+ i$ d( ]
just
- b! z" k2 u+ @4 H r9 o+ h5 e) vtesting |
|