c语言的字符串分解——strtok函数和strsep函数.doc_第1页
c语言的字符串分解——strtok函数和strsep函数.doc_第2页
c语言的字符串分解——strtok函数和strsep函数.doc_第3页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

c语言的字符串分解strtok函数和strsep函数 今天才知道原来C语言也有类似java的split的函数,按特定字符串对字符串进行分解: strtok的解释和示例: strtok Syntax: view plain #include char *strtok( char *str1, const char *str2 ); The strtok() function returns a pointer to the next token in str1, where str2 contains the delimiters that determine the token. strtok() returns NULL if no token is found. In order to convert a string to tokens, the first call to strtok() should have str1 point to the string to be tokenized. All calls after this should have str1 be NULL. For example: view plain char str = now # is the time for all # good men to come to the # aid of their country; char delims = #; char *result = NULL; result = strtok( str, delims ); while( result != NULL ) printf( result is %sn, result ); result = strtok( NULL, delims ); The above code will display the following output: view plain result is now result is is the time for all result is good men to come to the result is aid of their country 在s中找出以ct中的字符为分隔的字符串,即是源串中除去了含有分隔串中的所有字符后余下的一段段的字符串,每调用一次找到一串,找不到则返回空串。第一次调用必须传给它有效的字符串,第二次传NULL就可以了,每次调用返回找到的子串的时候都会把源串中该子串的尾部字符(原来是搜索串中的某一字符)修改成/0字符返回值为每次调用得到的字串。 但是百度百科里面又有提到说“最新的Linux内核2.6.29,说明了这个函数已经不再使用,由速度更快的strsep()代替”。所以又去查了下strsep函数: 原型:char *strsep(char *stringp, const char *delim); 功能:分解字符串为一组字符串。从stringp指向的位置起向后扫描,遇到delim指向位置的字符后,将此字符替换为NULL,返回stringp指向的地址。 但是strsep函数,在 Windows是没有支持的,我是在Linux环境,所以ok 给个例子: view plain char str2 = 2011/11/28; char *buf; char *token; buf = str2; while(to

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论