Main Page | Class List | File List | Class Members

stringutils.h

00001 #ifndef __STRINGUTILS_H_ 00002 #define __STRINGUTILS_H_ 00003 00004 #include <string> 00005 #include <vector> 00006 00007 using namespace std; 00008 00009 class StringUtils 00010 { 00011 00012 public: 00013 static void Tokenize(const string& str, 00014 vector<string>& tokens, 00015 const string& delimiters = " ") 00016 { 00017 // Skip delimiters at beginning. 00018 string::size_type lastPos = str.find_first_not_of(delimiters, 0); 00019 // Find first "non-delimiter". 00020 string::size_type pos = str.find_first_of(delimiters, lastPos); 00021 00022 while (string::npos != pos || string::npos != lastPos) 00023 { 00024 // Found a token, add it to the vector. 00025 tokens.push_back(str.substr(lastPos, pos - lastPos)); 00026 // Skip delimiters. Note the "not_of" 00027 lastPos = str.find_first_not_of(delimiters, pos); 00028 // Find next "non-delimiter" 00029 pos = str.find_first_of(delimiters, lastPos); 00030 } 00031 } 00032 00033 static void split(const string& str, vector<string>*out, const string& delim=",") 00034 { 00035 string::size_type lpos = 0; 00036 string::size_type pos = str.find_first_of(delim, lpos); 00037 while(lpos != string::npos) 00038 { 00039 out->insert(out->end(), str.substr(lpos,pos - lpos)); 00040 lpos = ( pos == string::npos ) ? string::npos : pos + 1; 00041 pos = str.find_first_of(delim, lpos); 00042 } 00043 } 00044 }; 00045 #endif

Generated on Mon Jan 8 23:19:06 2007 for librf by doxygen 1.3.7