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

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

[复制链接]

该用户从未签到

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

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
  • 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-11-25 23:06 , Processed in 0.156250 second(s), 24 queries , Gzip On.

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

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

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