找回密码
 注册
关于网站域名变更的通知
查看: 294|回复: 1
打印 上一主题 下一主题

了解一下C语言字符串的拆分函数strtok

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2020-3-26 09:57 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

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
  • TA的每日心情

    2019-11-29 15:37
  • 签到天数: 1 天

    [LV.1]初来乍到

    2#
    发表于 2020-3-26 18:26 | 只看该作者
    了解一下C语言字符串的拆分函数strtok
    您需要登录后才可以回帖 登录 | 注册

    本版积分规则

    关闭

    推荐内容上一条 /1 下一条

    EDA365公众号

    关于我们|手机版|EDA365电子论坛网 ( 粤ICP备18020198号-1 )

    GMT+8, 2025-10-25 13:49 , Processed in 0.140625 second(s), 24 queries , Gzip On.

    深圳市墨知创新科技有限公司

    地址:深圳市南山区科技生态园2栋A座805 电话:19926409050

    快速回复 返回顶部 返回列表