75 lines
1.8 KiB
C++
75 lines
1.8 KiB
C++
#include "TextUtility.h"
|
|
#include <string>
|
|
#include <cctype>
|
|
|
|
std::string TextUtility::capitalize(const std::string &str) {
|
|
std::string ret;
|
|
bool nex = false;
|
|
auto it = str.begin();
|
|
ret.push_back(std::toupper(*it));
|
|
it++;
|
|
while (it != str.end()) {
|
|
if (nex) {
|
|
ret.push_back(std::toupper(*it));
|
|
nex = false;
|
|
} else {
|
|
ret.push_back(*it);
|
|
}
|
|
if ('.' == *it || '!' == *it || '?' == *it) nex = true;
|
|
it++;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
std::string TextUtility::toUpperCase(const std::string &str) {
|
|
std::string ret;
|
|
auto it = str.begin();
|
|
while (it != str.end()) {
|
|
ret.push_back(std::toupper(*it));
|
|
it++;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
bool TextUtility::isNumeric(const std::string &str) {
|
|
auto it = str.begin();
|
|
while (it != str.end() && std::isdigit(*it)) ++it;
|
|
return !str.empty() && it == str.end();
|
|
}
|
|
|
|
int TextUtility::contains(const std::string &str, const std::string &substr) {
|
|
std::size_t found = str.find(substr);
|
|
if (found != std::string::npos) return found;
|
|
return -1;
|
|
}
|
|
|
|
std::string TextUtility::addSpaces(const std::string &str) {
|
|
std::string ret;
|
|
auto it = str.begin();
|
|
while (it != str.end()) {
|
|
ret.push_back(*it);
|
|
ret.push_back(' ');
|
|
it++;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
std::string TextUtility::removeDuplicatedSpaces(const std::string &str) {
|
|
std::string ret;
|
|
bool next = false;
|
|
auto it = str.begin();
|
|
while (it != str.end()) {
|
|
if (next) {
|
|
if(*it != ' ') {
|
|
ret.push_back(*it);
|
|
}
|
|
next = false;
|
|
} else {
|
|
ret.push_back(*it);
|
|
}
|
|
if (*it == ' ') next = true;
|
|
it++;
|
|
}
|
|
return ret;
|
|
}
|