54 for (
unsigned int i = 0; i < s.size(); i++)
57 if (c !=
' ' && c !=
'\n' && c !=
'\t')
68 for (
size_t i = 0; i < s.size(); i++)
70 result +=
static_cast<char>(toupper(static_cast<int>(s[i])));
80 for (
size_t i = 0; i < s.size(); i++)
82 result +=
static_cast<char>(tolower(static_cast<int>(s[i])));
106 for (
unsigned int i = 0; i < st.size(); i++)
108 if (isWhiteSpaceCharacter(st[i]))
110 st.erase(st.begin() + i);
126 while (st.size() > 0 && isWhiteSpaceCharacter(st[0]))
128 st.erase(st.begin());
142 while (st.size() > 0 && isWhiteSpaceCharacter(st[st.size() - 1]))
144 st.erase(st.end() - 1);
155 return removeFirstWhiteSpaces(removeLastWhiteSpaces(s));
174 for (
unsigned int i = 0; i < st.size(); i++)
176 if (isNewLineCharacter(st[i]))
178 st.erase(st.begin() + i);
194 while (st.size() > 0 && isNewLineCharacter(st[st.size() - 1]))
196 st.erase(st.end() - 1);
207 if (c ==
'0' || c ==
'1' || c ==
'2' || c ==
'3' || c ==
'4' 208 || c ==
'5' || c ==
'6' || c ==
'7' || c ==
'8' || c ==
'9')
224 if (s[0] ==
'-') i = 1;
225 for (; i < s.size(); ++i)
230 else if (c == scientificNotation) {
232 if (i == s.size() - 1)
return false;
234 if (c ==
'-' || c ==
'+') i++;
235 if (i == s.size() - 1)
return false;
236 if (sepCount == 0) sepCount = 1;
237 }
else if (!isDecimalNumber(c))
239 if (sepCount > 1 || sciCount > 1)
254 if (s[0] ==
'-') i = 1;
255 for (; i < s.size(); ++i)
258 if (c == scientificNotation) {
260 if (i == s.size() - 1)
return false;
262 if (c ==
'-')
return false;
264 if (i == s.size() - 1)
return false;
265 }
else if (!isDecimalNumber(c))
296 oss << setprecision(precision) << d;
304 if (!isDecimalInteger(s, scientificNotation))
throw Exception(
"TextTools::toInt(). Invalid number specification: " + s);
305 istringstream iss(s);
315 if (!isDecimalNumber(s, dec, scientificNotation))
throw Exception(
"TextTools::toDouble(). Invalid number specification: " + s);
316 istringstream iss(s);
326 if (s.size() > newSize)
327 return s.substr(0, newSize);
329 return s + string(newSize - s.size(), fill);
336 if (s.size() > newSize)
337 return s.substr(s.size() - newSize);
339 return string(newSize - s.size(), fill) + s;
348 while (tmp.size() > n)
350 v.push_back(tmp.substr(0, n));
365 for (
size_t i = 0; i < s.size(); i++)
368 if (current == blockBeginning)
371 t += s.substr(begPos, i - begPos);
373 else if (current == blockEnding)
380 else if (blockCount < 0)
381 throw Exception(
"TextTools::removeSubstrings(). " +
382 string(
"Ending block character without corresponding beginning one at position ") + toString((
int)i) +
".");
385 t += s.substr(begPos);
391 std::string
TextTools::removeSubstrings(
const std::string& s,
char blockBeginning,
char blockEnding, std::vector<string>& exceptionsBeginning, std::vector<string>& exceptionsEnding)
397 for (
size_t i = 0; i < s.size(); i++)
400 if (current == blockBeginning)
403 for (
size_t j = 0; j < exceptionsBeginning.size(); j++)
405 size_t pos = exceptionsBeginning[j].find(blockBeginning);
406 if (pos != string::npos) {
407 size_t left = i - pos;
408 size_t right = i + exceptionsBeginning[j].length() - pos;
409 if ((right < s.length() - 1) && (hasSubstring (s.substr(left, right), exceptionsBeginning[j])))
419 t += s.substr(begPos, i - begPos);
422 else if ( (current == blockEnding) && (blockCount > 0) )
424 for (
size_t j = 0; j < exceptionsEnding.size(); j++)
426 size_t pos = exceptionsEnding[j].find(blockEnding);
427 if (pos != string::npos) {
428 size_t left = i - pos;
429 size_t right = i + exceptionsEnding[j].length() - pos;
430 if ((right < s.length() - 1 ) && (hasSubstring (s.substr(left, right), exceptionsEnding[j])))
441 else if (blockCount < 0)
442 throw Exception(
"TextTools::removeSubstrings(). " +
443 string(
"Ending block character without corresponding beginning one at position ") + toString((
int)i) +
".");
446 t += s.substr(begPos);
458 for (
unsigned int i = 0; i < st.size(); i++)
462 st.erase(st.begin() + i);
475 unsigned int count = 0;
476 string::size_type index = s.find(pattern);
477 while (index != string::npos)
480 index = s.find(pattern, index + 1);
489 if (s.size() < pattern.size())
491 return s.substr(0, pattern.size()) == pattern;
498 if (s.size() < pattern.size())
500 return s.substr(s.size() - pattern.size()) == pattern;
507 if (s.size() < pattern.size())
509 for (
size_t i = 0; i < s.size() - pattern.size() + 1; ++i)
511 if (s.substr(i, pattern.size()) == pattern)
523 size_t pos = target.find(query);
524 while (pos != string::npos) {
525 target.replace(pos, query.length(), replacement);
526 pos += replacement.length();
527 pos = target.find(query, pos);
static bool isNewLineCharacter(char c)
Tell if a character is a new line character or not.
static std::string resizeLeft(const std::string &s, size_t newSize, char fill=' ')
Send a string of size 'newSize', which is a copy of 's' truncated or filled with character 'fill' at ...
This class allows to perform a correspondence analysis.
static std::string resizeRight(const std::string &s, size_t newSize, char fill=' ')
Send a string of size 'newSize', which is a copy of 's' truncated or filled with character 'fill' at ...
static bool isDecimalNumber(char c)
Tell is a given character describes a decimal number.
static std::string toString(T t)
General template method to convert to a string.
static int toInt(const std::string &s, char scientificNotation='e')
Convert from string to int.
static unsigned int count(const std::string &s, const std::string &pattern)
Count the occurences of a given pattern in a string.
static bool startsWith(const std::string &s, const std::string &pattern)
Tell is a string begins with a certain motif.
static bool hasSubstring(const std::string &s, const std::string &pattern)
Tell is a string contains a certain motif.
static bool isEmpty(const std::string &s)
Tell if a string is empty.
static std::string removeSurroundingWhiteSpaces(const std::string &s)
Remove all white spaces characters at the beginning and the end of a string.
static bool endsWith(const std::string &s, const std::string &pattern)
Tell is a string ends with a certain motif.
static std::string removeNewLines(const std::string &s)
Remove all new line characters in a string.
static std::string removeWhiteSpaces(const std::string &s)
Remove all white spaces characters in a string.
static bool isDecimalInteger(const std::string &s, char scientificNotation='e')
Tell is a given character string describes a decimal integer.
static std::string removeFirstWhiteSpaces(const std::string &s)
Remove all white spaces characters at the beginning of a string.
static std::string removeLastWhiteSpaces(const std::string &s)
Remove all white spaces characters at the end of a string.
static std::string removeLastNewLines(const std::string &s)
Remove all new line characters at the end of a string.
static std::vector< std::string > split(const std::string &s, size_t n)
Split a string into parts of size 'n'.
static std::string removeSubstrings(const std::string &s, char blockBeginning, char blockEnding)
Remove substrings from a string.
static std::string removeChar(const std::string &s, char c)
Remove all occurences of a character in a string.
static std::string toUpper(const std::string &s)
Make the string uppercase.
static std::string toLower(const std::string &s)
Make the string lowercase.
static void replaceAll(std::string &target, const std::string &query, const std::string &replacement)
Replacement of all non-overlapping occurrences of a certain motif in a string.
static double toDouble(const std::string &s, char dec='.', char scientificNotation='e')
Convert from string to double.
static bool isWhiteSpaceCharacter(char c)
Tell if a character is a white space or not.