Usage:string s = "Hello world";vector words = strings::split(s);//words now contains "Hello" and "world"string s = "1.0";float f = strings::toFloat(s);//f now holds 1.0string s = "#some comment";if (strings::startsWith(s, "#")) { continue; //This if statement will be entered}etc.
strings.h
#ifndef STRING_H_INCLUDED#define STRING_H_INCLUDED#include #include using std::string;using std::vector;namespace strings { bool startsWith(const string& s, const string& s2); bool endsWith(const string& s, const string& s2); vector split(string str, string delim = " "); void ljust(string& s, uint length, char padChar = ' '); void rjust(string& s, uint length, char padChar = ' '); string strip(const string& s); void replace(string& s, const string& find, const string& replace); float toFloat(const string& s); int toInt(const string& s); int findCountOf(const string& s, const string& word);}#endif // STRING_H_INCLUDED
strings.cpp
#include #include #include #include using std::string;using std::vector;using std::istringstream;namespace strings { /** Returns true if s starts with the string s2 */ bool startsWith(const string& s, const string& s2) { return s.find(s2) == 0; } /** Returns true if s ends with the string s2 */ bool endsWith(const string& s, const string& s2) { return s.find_last_of(s2) == (s.length() - s2.length()); } /** Splits a string on the delimeter and returns each part in a vector. */ vector split(string str, string delim = " ") { string::size_type cutAt; vector results; while( (cutAt = str.find_first_of(delim)) != string::npos ) { if(cutAt > 0) { results.push_back(str.substr(0,cutAt)); } str = str.substr(cutAt+1); } if (str.length() > 0) { results.push_back(str); } return results; } /** Adds padding characters to the left of a string until the string is a certain length. If the string is longer than the length passed then the string is left unchanged */ void ljust(string& s, uint length, char padChar = ' ') { std::string::difference_type whitespace = length - s.size(); if (whitespace > 0) s = s + string (whitespace, padChar); return s; } void rjust(string& s, uint length, char padChar = ' ') { std::string::difference_type whitespace = length - s.size(); if (whitespace > 0) s = string (whitespace, padChar) + s; } /** Strips spaces from the beginning and end of a string */ string strip(const string& s) { string sep = " "; const std::string::size_type first = s.find_first_not_of(sep); return (first == std::string::npos) ? s : s.substr(first, s.find_last_not_of(sep) - first + 1); } /** Replace occurrances of 'find' with 'replace' in 's' */ void replace(string& s, const string& find, const string& replace) { string::size_type j; for (; (j = s.find(find)) != string::npos;) { s.replace(j, find.length(), replace); } } /** Attempts a type-safe cast from string to float, throws a std::runtime_error on failure */ float toFloat(const string& s) { float result; istringstream iss(s); if ((iss >> std::dec >> result).fail()) { throw std::runtime_error("Error converting string to float"); } return result; } /** Attempts a type-safe cast from string to integer, throws a std::runtime_error on failure */ int toInt(const string& s) { int result; istringstream iss(s); if ((iss >> std::dec >> result).fail()) { throw std::runtime_error("Error converting string to integer"); } return result; } /** Find the number of occurances of a word in a string (s) */ int findCountOf(const string& s, const string& word) { int count = 0; string::size_type wordPos = 0; while (wordPos != string::npos) { wordPos = s.find(word, wordPos); if (wordPos != string::npos) { ++count; // start next search after this word wordPos += word.length(); } } return count; }}