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

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

[复制链接]

该用户从未签到

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

EDA365欢迎您登录!

您需要 登录 才可以下载或查看,没有帐号?注册

x

- n& k: S: |8 qchar *  strtok ( char * string, const char * delimiters );+ B/ C0 |$ \, u& E) N8 O% a& E6 t

' b) z8 H( G. Z  A9 }- [5 m1 sSequentially truncate string if delimiter is found.1 R" ^! j8 V* q
  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.
* M8 l2 a3 o0 a! k* G1 N' s# 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.
% x; x( V$ Q9 a4 |9 T; l  delimiters may vary from a call to another.9 w9 \* @6 {8 X5 f) _
* U0 T7 g" {1 `# P
Parameters.
! f$ w: n+ B' d8 z5 h6 y* k4 ~% ~9 _3 J  p% ]* x7 n$ ]( \8 g4 d; K& h
string6 C3 O7 s5 n# [( {" e
Null-terminated string to scan.' f& K8 r. K% i4 K) K
separator
2 H' F9 [8 o, ?( JNull-terminated string containing the separators.4 Q. q3 p  J4 e# m3 \0 {
Return Value.$ W3 u1 W* `+ k! L
  A pointer to the last token found in string.   NULL is returned when there are no more tokens to be found.
& R3 o$ _* h: k9 T$ {/ g1 H9 T5 E3 D% L' s, R! x
Portability.
& j# {  n; p: |. q' F0 r  Defined in ANSI-C.
4 O  n$ x8 E0 C6 j# N, a/ M2 |
Example.( u' ]* z# {0 a- c4 D

2 w) a7 H! n8 @$ y- z/ C% A. F/* strtok example */8 w7 M2 `) E' k% ^" G9 I* _* z- f
#include <stdio.h>
# u: H: c8 F# r1 }6 A/ j#include <string.h>
) A# S# Z" R* B' }' }2 U/ T1 \: u5 U( |4 _1 M
int main ()% K; ?0 ?0 T5 |/ e0 Q, U
{( X( o1 Y' n5 _0 ~6 B. H/ O
  char str[] ="This is a sample string,just testing.";
" }( o. r, a6 O# Y+ e+ m0 l7 I  char * pch;
4 D( w" M+ o$ \* ^! D  printf ("Splitting string \"%s\" in tokens:\n",str);
$ ^; d1 J% S3 E: l3 v7 T2 j  pch = strtok (str," ");9 k5 A! J1 E# W
  while (pch != NULL)# ~* z* R3 V9 o6 O
  {
8 D; w3 d; P) {, y    printf ("%s\n",pch);
) I# {, m) D/ Q7 {3 `% N    pch = strtok (NULL, " ,.");5 s; ?- g1 M$ S# u8 h; @7 @6 v9 X4 a
  }
) T+ N$ `6 a6 F6 z  return 0;$ B/ \) i' o7 R$ i$ C) |8 l
}' H1 K) b8 i7 E& D( G
Output:( J/ t" G! B7 [* |6 s
Splitting string "This is a sample string,just testing." in tokens:8 u7 M9 r& K: f3 A4 r' T& y
This) i1 Q# b7 g; i
is( J' D! T/ W& r7 X
a
; D, }$ g( N% p4 Zsample* o, M- l4 \# }
string
/ z$ K, x3 `" Q0 a) X# _just
" d% r; l. O8 G) Ztesting
  • 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 21:29 , Processed in 0.156250 second(s), 24 queries , Gzip On.

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

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

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