一个关于特定分隔符分割字符串的问题(用strtok并不理想)
char GPS_INFO[]="$GPRMC,112720.000,A,3639.2868,N,11659.8396,E,,,200305,,*12"
用","为分隔标志将里面的指定参数信息提取出来,C语言怎么实现?
希望以如下函数形式实现
char *strSplict(char *str,const char *seps,int pos)
其中str是原字符串,seps为分隔符,pos为指定位置.
比如 info3=strSplict(GPS_INFO,",",3)应为"3639.2868";
我尝试过strtok,但很有意思的是遇到",,,"这样的空段它自作主张的给去掉了(我的意思是就算空茅坑它也必须得给我留着,好让我跟序号一一对上)
相关推荐
经典的C语言字符分割文档
主要介绍如何分割带有逗号或者其他符号字符串,分割成字符串数组
C++ 字符串截取(strtok)
C++字符串截取,例如:"adda,dagf,dfa,df",将','去除,将每部分字符出入一个指定数组
C语言切割多层字符串(strtok_r strtok使用方法)
主要介绍了C语言切割多层字符串的方法,说了strtok的弱点,使用strtok_r的方法
strtok的缺陷,使用strtok_s函数更新安全度
strtok的缺陷,使用strtok_s函数更新安全度 #include "stdafx.h" #include "Windows.h" #include #include using namespace std; char string1[] = "A string\tof ,,tokens\nand some more tokens"; char string2[] = "Another string\n\tparsed at the same time."; char separators[] = " ,\t\n"; char *token1, *token2, *next_token1, *next_token2; int main(void) { cout << "Tokens:" << endl; // establish a string and get the first token: token1 = strtok_s(string1, separators, &next;_token1); token2 = strtok_s(string2, separators, &next;_token2); // while there are tokens in "string1" or "string2" while ((token1 != NULL)) { // get the next token: if (token1 != NULL) { cout << token1 << endl; token1 = strtok_s(NULL, separators, &next;_token1); OutputDebugString(_T(token1)); } } while ((token2 != NULL)) { // get the next token: if (token2 != NULL) { cout << token2 << endl; token2 = strtok_s(NULL, separators, &next;_token2); OutputDebugString(_T(token2)); } } Sleep(3000); system("pause");
strtok函数的使用示例
今天小编就为大家分享一篇关于strtok函数的使用示例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧


