C++ String Tokenizer
自己處理Token的話大概是這樣做,可以自己決定要哪些分割字元
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
using namespace std;
int main(int argc, char** argv)
{
string sLine = " abc : dsa\tda : 1112 ";
vector<string> tokens;
for (unsigned int n=0 ; n<sLine.size() ; ++n)
{
switch (sLine[n])
{
case '\t':
case ' ':
case ':':
sLine[n] = '\0';
break;
default:
break;
}
}
bool bIsToken = false;
for (unsigned int n=0 ; n<sLine.size() ; ++n)
{
if (sLine[n] && !bIsToken)
{
// add new token
tokens.push_back(sLine.c_str()+n);
bIsToken = true;
}
else if (bIsToken && !sLine[n])
{
bIsToken = false;
}
}
for (unsigned int n=0 ; n<tokens.size() ; ++n)
cout << "Token[" << n << "]='" << tokens[n] << "'" << endl;
return 0;
}
利用SrtingStream與getline函數來分割字串,這裡只能使用一個分割字元
#include <string>
#include <iostream>
#include <sstream>
#include <algorithm>
using namespace std;
int main()
{
char SepChar = '\n';
std::string str = "The quick brown fox\r\n aaaaa\r\b";
// construct a stream from the string
std::stringstream strstr(str);
// store the string token
std::string CurrPath;
while(getline(strstr, CurrPath, SepChar)) {
cout << CurrPath << endl;
}
return 0;
}
留言