C语言实现按分隔符来截取字符串
来源:互联网 发布:淘宝交易系统架构 编辑:程序博客网 时间:2024/05/17 02:46
问题描述:我们的系统通过Socket网络通讯往linux服务器上发送数据,服务器上接收的数据格式是以逗号隔开的字符串。我们需要将这个字符串按逗号作为分隔符来截取。
解决方法:使用C语言中的strtok()函数实现
代码实现(下面代码的功能是将字符串"now , is the time for all , good men to come to the , aid of their country"以逗号作为分隔符来截取,并将截取出的字符串打印出来):
#include <stdio.h>#include <string.h>int main(){ 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 \"%s\"\n", result ); result = strtok( NULL, delims ); }}运行结果如下:
进一步:封装成实现按分隔符截取字符串的函数
#include <stdio.h> #include <string.h> void split(char str[],char delims[]){ char *result = NULL; result = strtok( str, delims ); while( result != NULL ) { printf( "result is \"%s\"\n", result ); result = strtok( NULL, delims ); } }int main() { char str[] = "now , is the time for all , good men to come to the , aid of their country"; char delims[] = ","; split(str,delims);}
阅读全文
0 0
- C语言实现按分隔符来截取字符串
- C语言中通过分隔符来截取字符串
- [转]C语言中通过分隔符来截取字符串
- C语言中通过分隔符来截取字符串
- 【C语言】截取字符串实现
- 截取字符串 --C语言实现
- oracle字符串按制定分隔符截取
- Oracle函数,按分隔符截取字符串
- C语言简单实现字符串截取
- C语言实现的字符串截取函数
- c语言 字符串截取
- C语言截取字符串
- C语言 字符串截取
- C语言处理分隔字符串
- Oracle函数,按分隔符截取字符串 [问题点数
- oracle 根据分隔符截取字符串
- C语言实现字符串截取函数left、mid和right
- 数据结构学习(一)字符串截取的C语言实现
- windows图形化安装weblogic
- 2017年工作总结
- android TV中AlertDialog设置按键监听方法和禁止按键监听属性
- 全文搜索引擎 Elasticsearch 入门教程
- python爬虫提取数据之Beautifulsoup4简单使用
- C语言实现按分隔符来截取字符串
- mybatis逆向工程generatorConfig.xml配置(MySQL和SQL server)
- 设计模式之详解七种单例模式
- NGUI中UITextList的使用
- XMind,让你的双十二亮起来
- 所有面积公式
- iOS中代理属性为什么要用Weak修饰?
- 【Android学习】Window和WindowManager
- hdoj 6231 K-th Number