|
|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
: S0 N5 d2 ?/ m# q( b# j! J: y* `char * strtok ( char * string, const char * delimiters );
4 V. @( _8 Z8 R. M4 S# }# R* W8 u2 \0 a: A& C0 K+ ^5 `
Sequentially truncate string if delimiter is found.
; W4 v- E1 Y3 P' m' r- h8 @ 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.4 G5 `5 y+ j, Q- L; ~! e* B# f
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.
& Q9 G$ ^/ g' @" L7 `; ]3 V0 C delimiters may vary from a call to another.
+ F& f& D7 s/ J" t; W( N3 W+ ^
, r( g5 p v$ e" S+ W' c6 ~Parameters.
, E1 ?6 B" Y; {- a# C! G# G# [* F* ?* B
string
1 q" v7 i9 P& E. o1 P" G/ N* zNull-terminated string to scan.
' ~+ |. n, o7 Hseparator
% Z; g% K6 n2 U" v. u. yNull-terminated string containing the separators.
5 ?+ s3 _/ d% d+ c* x3 }Return Value.. a4 o/ C4 ~9 t z7 x1 c6 l6 C# [
A pointer to the last token found in string. NULL is returned when there are no more tokens to be found.
+ ]' V2 `- G9 P* B
1 @! O; G; ^: M) `5 ZPortability.
5 [1 P4 F2 v( v8 j+ k- ]0 [ Defined in ANSI-C.
2 k {# o6 Q. `8 F' Q5 Y% |% U8 t$ f
; r+ U" i+ q% ^3 i" j' ^" }! nExample.- V8 d) b% |7 e8 A5 h! p2 m
" {# E& y/ l( O3 J
/* strtok example */
" Z; }! A- n) d#include <stdio.h>( Q" c6 t1 y1 ~ d
#include <string.h>4 \; n* ?# c- t
, a( f! r' a& G. e. {int main (). P0 Y8 J) D8 D' h5 S& ?( `
{
% d- w, w+ T, _ Q# G/ K char str[] ="This is a sample string,just testing.";, c" }- n6 K, ^# [# K
char * pch;9 ]) {! h5 x& J
printf ("Splitting string \"%s\" in tokens:\n",str);
O {4 g8 z& S3 _' a$ r pch = strtok (str," ");! }. {" ^, k& s3 b
while (pch != NULL)4 R4 a i8 @! K& n3 X y
{
6 p9 B! G5 U: h% `! I printf ("%s\n",pch);9 g& z" V8 |% o( c
pch = strtok (NULL, " ,.");
8 K1 y+ U4 i* k }
' `) Q: H7 U6 w5 W return 0;
; c6 m E1 G. c+ R* c2 L4 p}: T1 f5 N1 j& I. j& z; ?
Output:
1 d1 E% V# G6 E' d, ^5 ^+ ]. {Splitting string "This is a sample string,just testing." in tokens:( s" H( ] _/ \ s6 Y* G) n+ |; @
This
+ _& G1 t/ O" p& g3 iis
- f. d* E6 [+ |4 S x* z, `* O2 ra
8 R5 ?( X* R0 E$ h' {) hsample
1 l8 ^: T& a, c# L: L& H1 o5 dstring: u+ f: d3 G1 q7 ?8 Y* W: u
just/ g" q7 q; K' i
testing |
|