/***************************************************************** file: string_ext.hh version: 2003-09-30 author: Steffen Brinkmann license: GPL contents: inline int split(const string &src, list &dest, const string delims=" \t\n") inline int split(const string &src, vector &dest, const string delims=" \t\n") inline int split(const string &src, vector &dest, const string delims=" \t\n") inline int split(const string &src, vector &dest, const string delims=" \t\n") inline string toupper(const string& s) inline string tolower(const string& s) inline string itos(const int i) comment: The split funcions are inspired by the perl function of this name. They split a string (e.g. a file you just read) into a vector or list of either strings, doubles or ints and return the number of items. toupper(..) and tolower(..) implement the ctype-functions of the same name for strings. itos(..) replaces the function itoa() which is not implemented in all libraries. It uses a stringstream to convert a int to a string as implemented by Bjarne Stroustrup in his C++ Style and Technique FAQ All functions are inlined so you don't have to explicitely compile this file. Just include it into your application. Download the source from http://lsw.uni-heidelberg.de/users/sbrinkma/cpp/string_ext.hh Comments and suggestions are welcome! Hyriand (2004-02-20): added str_replace ******************************************************************/ #ifndef STRING_EXT_HH #define STRING_EXT_HH #include #include #include #include #include // Split a string into a list of strings inline unsigned int split(const std::string &src, std::list &dest, const std::string delims=" \t\n") { unsigned int i=0,j=0,n=0; while(j!=std::string::npos) { i=src.find_first_not_of(delims,j); if (i==std::string::npos) break; j=src.find_first_of(delims,i); dest.push_back(src.substr(i,j-i)); n++; } return n; } // Split a string into a vector of strings inline unsigned int split(const std::string &src, std::vector &dest, const std::string delims=" \t\n") { unsigned int i=0,j=0,n=0; while(j!=std::string::npos) { i=src.find_first_not_of(delims,j); if (i==std::string::npos) break; j=src.find_first_of(delims,i); dest.push_back(src.substr(i,j-i)); n++; } return n; } // Split a string into a vector of doubles inline unsigned int split(const std::string &src, std::vector &dest, const std::string delims=" \t\n") { unsigned int i=0,j=0,n=0; while(j!=std::string::npos) { i=src.find_first_not_of(delims,j); if (i==std::string::npos) break; j=src.find_first_of(delims,i); dest.push_back(atof(src.substr(i,j-i).data())); n++; } return n; } // Split a string into a vector of ints inline unsigned int split(const std::string &src, std::vector &dest, const std::string delims=" \t\n") { unsigned int i=0,j=0,n=0; while(j!=std::string::npos) { i=src.find_first_not_of(delims,j); if (i==std::string::npos) break; j=src.find_first_of(delims,i); dest.push_back(atoi(src.substr(i,j-i).data())); n++; } return n; } // Make an uppercase copy of s inline std::string toupper(const std::string& s) { std::string upper(s); for(size_t i=0; i