52 #define ID_STRINGUTILS "$Id: string_utilities.h,v 1.24 2015/08/16 16:06:28 bcoconni Exp $" 68 extern std::string& trim_left(std::string& str);
69 extern std::string& trim_right(std::string& str);
70 extern std::string& trim(std::string& str);
71 extern std::string& trim_all_space(std::string& str);
72 extern std::string& to_upper(std::string& str);
73 extern std::string& to_lower(std::string& str);
74 extern bool is_number(
const std::string& str);
75 std::vector <std::string> split(std::string str,
char d);
81 #if !defined(_LIBCPP_VERSION) && _MSC_VER < 1700 && __cplusplus < 201103L 82 extern std::string to_string(
int);
83 extern std::string to_string(
double);
84 extern std::string to_string(
float);
87 extern std::string replace(std::string str,
const std::string& old,
const std::string& newstr);
91 std::string& trim_left(std::string& str)
93 while (!str.empty() && isspace((
unsigned char)str[0])) {
99 std::string& trim_right(std::string& str)
101 while (!str.empty() && isspace((
unsigned char)str[str.size()-1])) {
102 str = str.erase(str.size()-1,1);
107 std::string& trim(std::string& str)
109 if (str.empty())
return str;
110 std::string temp_str = trim_right(str);
111 return str = trim_left(temp_str);
114 std::string& trim_all_space(std::string& str)
116 for (
size_t i=0; i<str.size(); i++) {
117 if (isspace((
unsigned char)str[i])) {
118 str = str.erase(i,1);
125 std::string& to_upper(std::string& str)
127 for (
size_t i=0; i<str.size(); i++) str[i] = toupper(str[i]);
131 std::string& to_lower(std::string& str)
133 for (
size_t i=0; i<str.size(); i++) str[i] = tolower(str[i]);
137 bool is_number(
const std::string& str)
142 return (str.find_first_not_of(
"+-.0123456789Ee") == std::string::npos);
145 std::vector <std::string> split(std::string str,
char d)
147 std::vector <std::string> str_array;
149 std::string temp =
"";
153 while (index != std::string::npos) {
154 temp = str.substr(0,index);
156 if (!temp.empty()) str_array.push_back(temp);
157 str = str.erase(0,index+1);
162 if (!temp.empty()) str_array.push_back(temp);
172 #if !defined(_LIBCPP_VERSION) && _MSC_VER < 1700 && __cplusplus < 201103L 173 std::string to_string(
int i)
176 sprintf(buffer,
"%d", i);
177 return std::string(buffer);
180 std::string to_string(
float x)
182 std::ostringstream o;
183 if (!(o << x)) std::cerr <<
"Bad float to string conversion" << std::endl;
187 std::string to_string(
double x)
189 std::ostringstream o;
190 if (!(o << x)) std::cerr <<
"Bad double to string conversion" << std::endl;
195 std::string replace(std::string str,
const std::string& oldstr,
const std::string& newstr)
197 std::string temp = str;
198 size_t old_idx = str.find(oldstr);
199 if (old_idx != std::string::npos) {
200 temp = str.replace(old_idx, 1, newstr);