4 This is a collection of string helper functions that works
5 together with string (and later also with STL String. Some of these
6 would certainly benefit from a rewrite/optimization.
25 int compare_no_case(string const & s, string const & s2);
28 int compare_no_case(string const & s, string const & s2, unsigned int len);
32 int compare(char const * a, char const * b)
39 int compare(char const * a, char const * b, unsigned int len)
41 return strncmp(a, b, len);
45 bool isStrInt(string const & str);
48 int strToInt(string const & str);
51 bool isStrDbl(string const & str);
54 double strToDbl(string const & str);
57 char lowercase(char c);
60 char uppercase(char c);
63 string const lowercase(string const &);
66 string const uppercase(string const &);
68 /// convert T to string
71 string const tostr(T const & t)
73 std::ostringstream ostr;
75 return ostr.str().c_str();
76 // We need to use the .c_str since we sometimes are using
77 // our own string class and that is not compatible with
78 // basic_string<char>. (of course we don't want this later)
84 string const tostr(bool b)
86 return (b ? "true" : "false");
89 /// Does the string start with this prefix?
90 bool prefixIs(string const &, char const *);
92 /// Does the string end with this char?
93 bool suffixIs(string const &, char);
95 /// Does the string end with this suffix?
96 bool suffixIs(string const &, char const *);
99 bool contains(char const * a, string const & b);
102 bool contains(string const & a, char const * b);
105 bool contains(string const & a, string const & b);
108 bool contains(char const * a, char const * b);
111 bool containsOnly(string const &, char const *);
114 bool containsOnly(string const &, string const &);
117 bool containsOnly(char const *, char const *);
120 bool containsOnly(char const *, string const &);
122 /// Counts how many of character c there is in a
123 unsigned int countChar(string const & a, char c);
125 /** Extracts a token from this string at the nth delim.
126 Doesn't modify the original string. Similar to strtok.
128 #"a;bc;d".token(';', 1) == "bc";#
129 #"a;bc;d".token(';', 2) == "d";#
131 string const token(string const & a, char delim, int n);
134 /** Search a token in this string using the delim.
135 Doesn't modify the original string. Returns -1 in case of
138 #"a;bc;d".tokenPos(';', "bc") == 1;#
139 #"a;bc;d".token(';', "d") == 2;#
141 int tokenPos(string const & a, char delim, string const & tok);
144 /** Compares a string and a (simple) regular expression
145 The only element allowed is "*" for any string of characters
147 bool regexMatch(string const & a, string const & pattern);
149 /// Substitute all "oldchar"s with "newchar"
150 string const subst(string const & a, char oldchar, char newchar);
152 /// Substitutes all instances of oldstr with newstr
153 string const subst(string const & a,
154 char const * oldstr, string const & newstr);
156 /** Strips characters off the end of a string.
157 #"abccc".strip('c') = "ab".#
159 string const strip(string const & a, char c = ' ');
161 /** Strips characters of the beginning of a string.
162 #"cccba".frontstrip('c') = "ba"#. */
163 string const frontStrip(string const & a, char c = ' ');
165 /** Strips characters off the beginning of a string.
166 #"ababcdef".frontstrip("ab") = "cdef"# .*/
167 string const frontStrip(string const & a, char const * p);
169 /** Splits the string by the first delim.
170 Splits the string by the first appearance of delim.
171 The leading string up to delim is returned in piece (not including
172 delim), while the original string is cut from after the delimiter.
174 #s1= ""; s2= "a;bc".split(s1, ';') -> s1 == "a"; s2 == "bc";#
176 string const split(string const & a, string & piece, char delim);
178 /// Same as split but does not return a piece
179 string const split(string const & a, char delim);
181 /// Same as split but uses the last delim.
182 string const rsplit(string const & a, string & piece, char delim);