ofen/src/of_str.cpp

44 lines
1.1 KiB
C++

#include "of_str.h"
namespace ofen {
COfStr::COfStr()
{
}
COfStr::~COfStr()
{
}
ofString COfStr::replace(const ofString& str, const ofString& from, const ofString& to)
{
if (from.empty()) {
return str;
}
ofString result = str;
size_t startPos = 0;
while ((startPos = result.find(from, startPos)) != ofString::npos) {
result.replace(startPos, from.length(), to);
startPos += to.length();
}
return result;
}
std::vector<ofString> COfStr::split(const ofString& input, const ofString& delimiter)
{
std::vector<ofString> result;
size_t pos = 0, prev = 0;
while ((pos = input.find(delimiter, prev)) != ofString::npos) {
result.push_back(input.substr(prev, pos - prev));
prev = pos + delimiter.size(); // Move past the delimiter
}
result.push_back(input.substr(prev));
return result;
}
ofString COfStr::trim(const ofString& input)
{
size_t start = input.find_first_not_of(" \t\n\r\f\v");
if (start == std::string::npos) {
return "";
}
size_t end = input.find_last_not_of(" \t\n\r\f\v");
return input.substr(start, end - start + 1);
}
} // namespace ofen