Hi All,
I wanted to know if there is a better way to split a string by a delimeter in C++ root. I have made a function below, but I can’t seem to figure out how to use TString.Tokenize(delimeter). In any case, I thought I would post this for future users, since I couldn’t find such an example online:
void PlayString(){
TString x="This,is,a,complicated,string";
TString delim=",";
vector<TString> v;
SplitString(x, delim, v);
cout<<v.size()<<endl;
for(int i=0; i<(int)v.size(); i++){
cout<<v.at(i)<<endl;
}
TString x="This<>is<>a,<>comasdfasdfd<>string<>asdfasdf<>oiukekdy";
TString delim="<>";
vector<TString> v;
SplitString(x, delim, v);
cout<<v.size()<<endl;
for(int i=0; i<(int)v.size(); i++){
cout<<v.at(i)<<endl;
}
}
void SplitString(TString x, TString delim, vector &v){
v.clear();
int stringLength=x.Length();
int delimLength=delim.Length();
cout<<"String: "<<x<<" "<<stringLength<<endl;
cout<<"Delim: "<<delim<<" "<<delimLength<<endl;
cout<<x<<" "<<stringLength<<endl;
int stop=1;
TString temp="---";
while(stop!=-1){
stop = x.First(delim);
if(stop!=-1){
temp = x(0, stop);
cout<<"Substring: "<<temp<<endl;
TSubString newString = x( stop+delimLength, stringLength );
x=newString;
stringLength=x.Length();
}
else{
stringLength=x.Length();
temp = x(0, stringLength);
cout<<"Substring: "<<temp<<endl;
}
v.push_back(temp);
}
}