Split TString by delimeter in root C++

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);
}

}

[code]TString x = “This,is,a,complicated,string”;
TObjArray *tx = x.Tokenize(",");
tx->Print();
for (Int_t i = 0; i < tx->GetEntries(); i++) std::cout << ((TObjString *)(tx->At(i)))->String() << std::endl;

TString y = “This<>is<>a,<>comasdfasdfd<>string<>asdfasdf<>oiukekdy”;
TObjArray *ty = y.Tokenize("<>"); // note: tokens separator = ANY CHARACTER in the delimiter string
ty->Print();
for (Int_t i = 0; i < ty->GetEntries(); i++) std::cout << ((TObjString *)(ty->At(i)))->String() << std::endl;[/code]