|
|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
X+ K/ p" l8 \! a# C/ ?char * strtok ( char * string, const char * delimiters );4 d; Y$ K" H G8 L3 k+ h: `
, j$ a t5 u" O$ f8 bSequentially truncate string if delimiter is found.
# S0 c; `7 Y. O8 [, Q5 I! h! I 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.
' g0 S7 o0 [# o" P* v% ? 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 @/ y3 v! D2 n! o6 h9 c1 n4 _5 N; R* x
delimiters may vary from a call to another.& z' d5 n: @/ J" }$ u! [. \
, v9 F; w0 M: A J5 w* w3 @Parameters.$ c/ Z) j4 Y% ^( C! Q
. F* L7 i! h' f- T! x/ n' V
string& m& }' D. [7 W& ~# G$ A
Null-terminated string to scan.) i8 i i: H" F3 y
separator3 K$ \ P0 y# w6 e: Z# i2 b
Null-terminated string containing the separators.
+ D- R, C; f9 [5 aReturn Value. b4 u% w+ Y( `' C; d( m
A pointer to the last token found in string. NULL is returned when there are no more tokens to be found.
. v8 m0 w* m8 L; f6 A/ x9 ?% [2 V5 O. V$ {
Portability.
6 C( E' G6 d9 R$ Y# @/ J! k) U' ] Defined in ANSI-C.
3 f% i. C! W$ _' O. t0 g# G6 a; d3 i3 `
Example.- y# H+ x) c: U* ?& @# _+ N
( T' | e% `8 J/* strtok example */
5 v0 ]3 N4 ^! b" L% s#include <stdio.h>4 c' ]& D! C, j7 }4 [+ M
#include <string.h>' T. O B* R* o. E4 `) R
/ |/ I5 D4 x6 Gint main ()$ C4 I# J/ u6 d) q3 V& W
{
& S( y2 J% f8 C$ ]8 J' T/ `- ^: l char str[] ="This is a sample string,just testing.";
$ l) d$ H& J- ?4 w4 R char * pch;
/ b$ t! p) V% i4 p6 I2 K printf ("Splitting string \"%s\" in tokens:\n",str);1 f3 ~' F, Z' U/ R
pch = strtok (str," ");" `- N* f4 Q1 @
while (pch != NULL)7 y. s+ Q1 i/ R" s+ f1 @! Q7 G. N
{
# F( V: r, x' G; Y4 i' w printf ("%s\n",pch);5 @4 t6 v& q* Z; P! p$ J# X
pch = strtok (NULL, " ,.");. T% Y$ q1 \2 Z: ]# j
}
! ?- V) f2 I( L0 C return 0;$ Q, u% W4 y; Z z) I
}
- R" t; L( g: ?2 X! O1 I0 R5 QOutput:
. s$ h% |" F) WSplitting string "This is a sample string,just testing." in tokens:' a w/ X g* j. D4 B4 L
This
, M8 |) l3 y* l! d) \! His! d. h8 p8 y E; ~* Y! c
a+ Y/ m0 F" x. F9 O+ o: T
sample; y6 Q- S( R- a, j4 l. z/ [5 {
string7 h4 }' n( @# ]& D2 f: f
just' N( |; ]& o) I, ~! F9 W
testing |
|