|
|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
+ j d5 e& O3 ^* {7 @( f$ n
char * strtok ( char * string, const char * delimiters );0 r, Q3 t6 I5 K2 }, s$ k/ B
5 `0 G! b3 _( X5 U
Sequentially truncate string if delimiter is found.
9 E& ?, g( U5 I# x* R( S 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.+ D9 K' c0 @ S9 ?- W& p
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.
1 v T! M& U; @# U0 D9 e delimiters may vary from a call to another.( \) f |8 I* {9 R! }; { P, K
4 r2 X! k. G7 ]2 p. r8 f6 i6 M8 QParameters.
; U) @& a1 O5 l3 q
& d0 X- t* d; V$ \5 f+ O Ostring
: B0 x: G6 @2 |4 ]( s9 aNull-terminated string to scan.
+ x- X @( k( }5 g1 Hseparator
; u2 E- a5 `/ cNull-terminated string containing the separators.
. |% p1 j9 v2 J: O' ]Return Value.
% ^; U t# f% @; c o A pointer to the last token found in string. NULL is returned when there are no more tokens to be found.
9 Z' F9 o R# @$ j1 {4 A2 y6 o+ C/ f3 o2 h
Portability.9 l# t( n+ Q, a1 @1 p, z% s
Defined in ANSI-C.
9 z+ I. R# F2 |6 l) |3 J0 O0 z: F' S
Example.
+ P+ H/ f6 |9 |$ \ i- V+ m2 l" _# [1 A# m+ ?% Z
/* strtok example */
8 U# I4 _" D# R' L; \* x5 ]#include <stdio.h>) H* x2 u- T& y- p$ a* d! c% m
#include <string.h>
( D9 ^" p/ `5 ]
/ Q- F9 S) z9 A7 g7 \int main ()
) c- K6 p# A9 Q0 p1 {6 n! X: y2 N$ z% I{
" O3 L' w; w2 T: ^2 O7 N/ r- ~ char str[] ="This is a sample string,just testing.";
1 [+ x6 s9 E1 K, I char * pch;9 o4 o1 f7 G2 v) t9 w
printf ("Splitting string \"%s\" in tokens:\n",str);. P7 k8 D& m3 Q9 t- k$ U" K
pch = strtok (str," ");1 g% f' H1 D% ?7 `
while (pch != NULL)
7 a0 T6 N2 F. n3 P {
, g% u: ]# `) C/ e( r printf ("%s\n",pch);
8 M: o) b& t3 ~; X. s# c" R. ]( { pch = strtok (NULL, " ,.");
: ~' p1 y0 h% l+ h6 D! r }
- @ V1 b' y* {0 j return 0;2 D! l0 ~ M3 T* c; g
}
' e5 e; T4 I0 i E" V- rOutput:
' G. p) N- B3 ZSplitting string "This is a sample string,just testing." in tokens:; |9 |$ X) m+ o) E( h' j* J8 u
This
/ Q& g5 f* M' jis
$ b$ z( M) n) H7 Ua
2 y+ a$ M6 p" J. Z% fsample/ M. H) h: D' m* ~; \8 w% I* n
string
! a" f! i: i+ @# _just
* M( z0 q6 E# F% }/ y2 utesting |
|