2
* WengoPhone, a voice over Internet phone
3
* Copyright (C) 2004-2006 Wengo
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
#include <util/owutildll.h>
30
* std::string wrapper/helper.
32
* Inspired from the class QString from the Qt library.
36
* @see java.lang.String
37
* @author Tanguy Krotoff
39
class String : public std::string {
45
* Rather than to code something like this:
47
* it's better to write:
48
* if (myString.empty())
49
* return "" -> return String::null
51
OWUTIL_API static const char * null;
54
* End of line character.
58
OWUTIL_API static const std::string EOL;
61
* Windows end of line character.
65
OWUTIL_API static const std::string WIN_EOL;
68
* Unix end of line character.
72
OWUTIL_API static const std::string UNIX_EOL;
75
* System end of line character.
77
* Equals \r\n or \n depending on the system (Windows, Unix...)
79
OWUTIL_API static const std::string SYSTEM_EOL;
81
OWUTIL_API String() : std::string() { }
83
OWUTIL_API String(const char * str) : std::string(str) { }
85
OWUTIL_API String(const std::string & str) : std::string(str) { }
88
* Converts this String to std::string.
90
* @return the converted String to std::string
92
OWUTIL_API operator const std::string&() {
97
* Converts this string to an int.
99
* @return the string converted to an int or 0 if failed to convert
101
OWUTIL_API int toInteger() const;
104
* Converts this string to a boolean.
106
* Detects strings: "true", "TRUE", "yes", "YES", "1"
107
* "false", "FALSE", "no", "NO", "0"
108
* Be very carefull when using this method, maybe it should throw an Exception.
110
* @return the string converted to a boolean (return false if failed to convert)
112
OWUTIL_API bool toBoolean() const;
115
* Converts all of the characters in this string to lower case.
119
* String myString("WengO");
120
* str = myString.toLowerCase(); //str == "wengo"
123
* @return the string converted to lowercase
125
OWUTIL_API std::string toLowerCase() const;
128
* Converts all of the characters in this string to upper case.
132
* String myString("WengO");
133
* str = myString.toUpperCase(); //str == "WENGO"
136
* @return the string converted to uppercase
138
OWUTIL_API std::string toUpperCase() const;
141
* @param str the string to test
142
* @return true if String begins with str
144
OWUTIL_API bool beginsWith(const String & str) const;
147
* @param str the string to test
148
* @return true if String ends with str
150
bool endsWith(const String & str) const;
153
* Gets the number of occurences of the string str inside this string.
155
* @param str string to find
156
* @param caseSensitive the search is case sensitive; otherwise the search is case insensitive
157
* @return true if this list contains the specified element
159
OWUTIL_API bool contains(const std::string & str, bool caseSensitive = true) const;
164
OWUTIL_API bool contains(char ch, bool caseSensitive = true) const;
167
* Replaces every occurence of the string before with the string after.
169
* @param before occurence to find
170
* @param after the string that will replace the string before
171
* @param caseSensitive the search is case sensitive; otherwise the search is case insensitive
173
OWUTIL_API void replace(const std::string & before, const std::string & after, bool caseSensitive = true);
176
* Appends a string onto the end of this string.
178
* @param str string to append
179
* @return the new string
181
OWUTIL_API std::string & append(const std::string & str);
184
* Removes every occurrence of str in the string.
186
* @param str to remove in the string
188
OWUTIL_API void remove(const std::string & str);
191
* Gets a string from a number.
193
* @param number number to convert into a string
194
* @return number converted to a string
196
OWUTIL_API static std::string fromNumber(int number);
199
* Gets a string from a boolean.
201
* @param boolean boolean to convert into a string
202
* @return boolean converted to a string
204
OWUTIL_API static std::string fromBoolean(bool boolean);
207
* Gets a string from an unsigned int.
209
* @param number unsigned int to convert into a string
210
* @return unsigned int converted to a string
212
OWUTIL_API static std::string fromUnsignedInt(unsigned int number);
215
* Gets a string from an double.
217
* @param number double to convert into a string
218
* @return double converted to a string
220
OWUTIL_API static std::string fromDouble(double number);
223
* Gets a string from a long.
225
* @param number long to convert into a string
226
* @return long converted to a string
228
OWUTIL_API static std::string fromLong(long number);
231
* Gets a string from a long long.
233
* @param number long long to convert into a string
234
* @return long long converted to a string
236
OWUTIL_API static std::string fromLongLong(long long number);
239
* Gets a string from a unsigned long long.
241
* @param number unsigned long long to convert into a string
242
* @return unsigned long long converted to a string
244
OWUTIL_API static std::string fromUnsignedLongLong(unsigned long long number);
247
* URL-encodes a string.
249
* @param url the string to encode
250
* @return a string with all non-alphanumeric characters replaced by their
251
* URL-encoded equivalent.
253
OWUTIL_API static std::string encodeUrl(const std::string & url);
256
* URL-decodes a string.
258
* @param url the URL-encoded string to decode
259
* @return a string with all URL-encoded sequences replaced by their
262
OWUTIL_API static std::string decodeUrl(const std::string & url);
265
* Tokenizes the string with a delimiter of your choice.
269
* String str("four");
270
* StringList tokens = str.split(""); //tokens = "four"
271
* String str("four roses");
272
* StringList tokens = str.split(" "); //tokens = "four", "roses"
275
* @param separator string delimiter
276
* @return tokens, strings created by splitting the input string
278
OWUTIL_API StringList split(const std::string & separator) const;
281
* Removes spaces at the beginning and the end of a string.
283
* @return a cleaned string
285
OWUTIL_API std::string trim();