怎么样将一个字符串切分成几段,并且这几段是不同的类型?例如 将 “20160519,21:23:00,100,210.,5,891282“ 这个长字符串切分为 字符串 20160519,字符串21:23:00,整型100,浮点型210.5,,,也就是按逗号切分,问一下大家有没有什么好的思路指导一下?
解决方案
3
_tcstok – strtok & wcstok
5
本人去实现的话也是简单的不行。假如你连这都不会写,只能说明基本还不过关:
#include <iostream> #include <algorithm> #include <vector> typedef std::vector<std::string> StringVector; static void StringSplitToVector(StringVector &strList, const char *pszSearch, char chDelim) { const char *pchDelim; while ((pchDelim = strchr(pszSearch, chDelim)) != NULL) { int nLength = pchDelim++ - pszSearch; std::string strItem(pszSearch, nLength); strList.push_back(strItem); pszSearch = pchDelim; } std::string strItem(pszSearch); strList.push_back(strItem); } int main(int argc, char *argv[]) { std::string str("20160519,21:23:00,100,210.,5,891282"); StringVector strList; StringSplitToVector(strList, str.c_str(), ","); for (size_t i = 0; i < strList.size(); i++) { std::cout << "[" << strList[i] << "]" << std::endl; } return 0; }
输出:
[20160519] [21:23:00] [100] [210.] [5] [891282]
3
3
本人去实现的话也是简单的不行。假如你连这都不会写,只能说明基本还不过关:
#include <iostream> #include <algorithm> #include <vector> typedef std::vector<std::string> StringVector; static void StringSplitToVector(StringVector &strList, const char *pszSearch, char chDelim) { const char *pchDelim; while ((pchDelim = strchr(pszSearch, chDelim)) != NULL) { int nLength = pchDelim++ - pszSearch; std::string strItem(pszSearch, nLength); strList.push_back(strItem); pszSearch = pchDelim; } std::string strItem(pszSearch); strList.push_back(strItem); } int main(int argc, char *argv[]) { std::string str("20160519,21:23:00,100,210.,5,891282"); StringVector strList; StringSplitToVector(strList, str.c_str(), ","); for (size_t i = 0; i < strList.size(); i++) { std::cout << "[" << strList[i] << "]" << std::endl; } return 0; }输出:
[20160519] [21:23:00] [100] [210.] [5] [891282]本人本人有实现,本人是在纠结能不能在切分的时候就把类型给转换了。
atoi转换为整型
atof转换为浮点型
8
仅供参考:
#include <stdio.h> #include <string.h> char string[80]; char seps1[3]; char seps2[3]; char *token; char *zzstrtok ( char *string, const char *control1,//连续出现时视为中间夹空token const char *control2 //连续出现时视为中间无空token ) { unsigned char *str; const unsigned char *ctrl1 = (const unsigned char *)control1; const unsigned char *ctrl2 = (const unsigned char *)control2; unsigned char map1[32],map2[32]; static char *nextoken; static char flag=0; unsigned char c; int L; memset(map1,0,32); memset(map2,0,32); do { map1[*ctrl1 >> 3] |= (1 << (*ctrl1 & 7)); } while (*ctrl1++); do { map2[*ctrl2 >> 3] |= (1 << (*ctrl2 & 7)); } while (*ctrl2++); if (string) { if (control2[0]) { L=strlen(string); while (1) { c=string[L-1]; if (map2[c >> 3] & (1 << (c & 7))) { L--; string[L]=0; } else break; } } if (control1[0]) { L=strlen(string); c=string[L-1]; if (map1[c >> 3] & (1 << (c & 7))) { string[L]=control1[0]; string[L+1]=0; } } str=(unsigned char *)string; } else str=(unsigned char *)nextoken; string=(char *)str; while (1) { if (0==flag) { if (!*str) break; if (map1[*str >> 3] & (1 << (*str & 7))) { *str=0; str++; break; } else if (map2[*str >> 3] & (1 << (*str & 7))) { string++; str++; } else { flag=1; str++; } } else if (1==flag) { if (!*str) break; if (map1[*str >> 3] & (1 << (*str & 7))) { *str=0; str++; flag=0; break; } else if (map2[*str >> 3] & (1 << (*str & 7))) { *str=0; str++; flag=2; break; } else str++; } else {//2==flag if (!*str) return NULL; if (map1[*str >> 3] & (1 << (*str & 7))) { str++; string=(char *)str; flag=0; } else if (map2[*str >> 3] & (1 << (*str & 7))) { str++; string=(char *)str; } else { string=(char *)str; str++; flag=1; } } } nextoken=(char *)str; if (string==(char *)str) return NULL; else return string; } void main() { strcpy(string,"A \tstring\t\tof ,,tokens\n\nand some more tokens, "); strcpy(seps1,",\n");strcpy(seps2," \t"); printf("\n[%s]\nTokens:\n",string); token=zzstrtok(string,seps1,seps2); while (token!=NULL) { printf(" <%s>",token); token=zzstrtok(NULL,seps1,seps2); } strcpy(string,"1234| LIYI|China | 010 |201110260000|OK"); strcpy(seps1,"|");strcpy(seps2," "); printf("\n[%s]\nTokens:\n",string); token=zzstrtok(string,seps1,seps2); while (token!=NULL) { printf(" <%s>",token); token=zzstrtok(NULL,seps1,seps2); } strcpy(string,"1234|LIYI||010|201110260000|OK"); strcpy(seps1,"");strcpy(seps2,"|"); printf("\n[%s]\nTokens:\n",string); token=zzstrtok(string,seps1,seps2); while (token!=NULL) { printf(" <%s>",token); token=zzstrtok(NULL,seps1,seps2); } strcpy(string,"1234|LIYI||010|201110260000|OK"); strcpy(seps1,"|");strcpy(seps2,""); printf("\n[%s]\nTokens:\n",string); token=zzstrtok(string,seps1,seps2); while (token!=NULL) { printf(" <%s>",token); token=zzstrtok(NULL,seps1,seps2); } strcpy(string,"a"); strcpy(seps1,",");strcpy(seps2,""); printf("\n[%s]\nTokens:\n",string); token=zzstrtok(string,seps1,seps2); while (token!=NULL) { printf(" <%s>",token); token=zzstrtok(NULL,seps1,seps2); } strcpy(string,"a,b"); strcpy(seps1,",");strcpy(seps2,""); printf("\n[%s]\nTokens:\n",string); token=zzstrtok(string,seps1,seps2); while (token!=NULL) { printf(" <%s>",token); token=zzstrtok(NULL,seps1,seps2); } strcpy(string,"a,,b"); strcpy(seps1,",");strcpy(seps2,""); printf("\n[%s]\nTokens:\n",string); token=zzstrtok(string,seps1,seps2); while (token!=NULL) { printf(" <%s>",token); token=zzstrtok(NULL,seps1,seps2); } strcpy(string,",a"); strcpy(seps1,",");strcpy(seps2,""); printf("\n[%s]\nTokens:\n",string); token=zzstrtok(string,seps1,seps2); while (token!=NULL) { printf(" <%s>",token); token=zzstrtok(NULL,seps1,seps2); } strcpy(string,"a,"); strcpy(seps1,",");strcpy(seps2,""); printf("\n[%s]\nTokens:\n",string); token=zzstrtok(string,seps1,seps2); while (token!=NULL) { printf(" <%s>",token); token=zzstrtok(NULL,seps1,seps2); } strcpy(string,",a,,b"); strcpy(seps1,",");strcpy(seps2,""); printf("\n[%s]\nTokens:\n",string); token=zzstrtok(string,seps1,seps2); while (token!=NULL) { printf(" <%s>",token); token=zzstrtok(NULL,seps1,seps2); } strcpy(string,",,a,,b,,"); strcpy(seps1,",");strcpy(seps2,""); printf("\n[%s]\nTokens:\n",string); token=zzstrtok(string,seps1,seps2); while (token!=NULL) { printf(" <%s>",token); token=zzstrtok(NULL,seps1,seps2); } strcpy(string,","); strcpy(seps1,",");strcpy(seps2,""); printf("\n[%s]\nTokens:\n",string); token=zzstrtok(string,seps1,seps2); while (token!=NULL) { printf(" <%s>",token); token=zzstrtok(NULL,seps1,seps2); } strcpy(string,",,"); strcpy(seps1,",");strcpy(seps2,""); printf("\n[%s]\nTokens:\n",string); token=zzstrtok(string,seps1,seps2); while (token!=NULL) { printf(" <%s>",token); token=zzstrtok(NULL,seps1,seps2); } strcpy(string,",,,"); strcpy(seps1,",");strcpy(seps2," "); printf("\n[%s]\nTokens:\n",string); token=zzstrtok(string,seps1,seps2); while (token!=NULL) { printf(" <%s>",token); token=zzstrtok(NULL,seps1,seps2); } } // //[A string of ,,tokens // //and some more tokens,] //Tokens: // <A>, <string>, <of>, <>, <tokens>, <>, <and>, <some>, <more>, <tokens>, <>, //[1234| LIYI|China | 010 |201110260000|OK] //Tokens: // <1234>, <LIYI>, <China>, <010>, <201110260000>, <OK>, //[1234|LIYI||010|201110260000|OK] //Tokens: // <1234>, <LIYI>, <010>, <201110260000>, <OK>, //[1234|LIYI||010|201110260000|OK] //Tokens: // <1234>, <LIYI>, <>, <010>, <201110260000>, <OK>, //[a] //Tokens: // <a>, //[a,b] //Tokens: // <a>, <b>, //[a,,b] //Tokens: // <a>, <>, <b>, //[,a] //Tokens: // <>, <a>, //[a,] //Tokens: // <a>, <>, //[,a,,b] //Tokens: // <>, <a>, <>, <b>, //[,,a,,b,,] //Tokens: // <>, <>, <a>, <>, <b>, <>, <>, //[,] //Tokens: // <>, <>, //[,,] //Tokens: // <>, <>, <>, //[,,,] //Tokens: // <>, <>, <>, <>,
用sscanf转换类型。
饭要一口一口地吃,事要一件一件地办。
10
分隔字符串之后,你需要本人去转换了。假如你想直接变成指定类型,你可以用 sscanf 来做。
#include <stdio.h> #include <stdlib.h> typedef struct { char date[32]; char time[32]; int ivalue; float fvalue; int unknown1; int unknown2; } UnknownData; int main(int argc, char *argv[]) { UnknownData data; const char *str = "20160519,21:23:00,100,210.,5,891282"; sscanf( str, "%[^,],%[^,],%d,%f,%d,%d", data.date, data.time, &data.ivalue, &data.fvalue, &data.unknown1, &data.unknown2 ); printf( "[%s]\n[%s]\n[%d]\n[%f]\n[%d]\n[%d]\n", data.date, data.time, data.ivalue, data.fvalue, data.unknown1, data.unknown2 ); return 0; }
输出:
[20160519] [21:23:00] [100] [210.000000] [5] [891282]
3
请检查sscanf的返回值。
5
//简单用boost::tokenizer,用std::getline也行
#include <iostream>
#include <algorithm>
#include <string>
#include <boost/tokenizer.hpp>
int main()
{
using namespace boost;
using namespace std;
string str = “20160519,21:23:00,100,210.,5,891282”;
tokenizer<char_separator<char>> ts(str, char_separator<char>(“,”));
copy(ts.begin(), ts.end(), ostream_iterator<string>(cout, “\n”));
}
#include <iostream>
#include <algorithm>
#include <string>
#include <boost/tokenizer.hpp>
int main()
{
using namespace boost;
using namespace std;
string str = “20160519,21:23:00,100,210.,5,891282”;
tokenizer<char_separator<char>> ts(str, char_separator<char>(“,”));
copy(ts.begin(), ts.end(), ostream_iterator<string>(cout, “\n”));
}