Files
hate/StringUtil.cpp

100 lines
1.7 KiB
C++
Raw Normal View History

2025-11-22 16:25:58 +01:00
#include "StringUtil.h"
std::vector<std::string> StringUtil::split(std::string str, std::string sep)
{
std::vector<std::string> vec;
while (str.size() != 0)
{
size_t index = str.find(sep);
if (index)
vec.push_back(str.substr(0, index));
else
vec.push_back("");
str = str.substr(index + sep.size());
}
return vec;
}
std::vector<std::string> StringUtil::split(std::string str, char sep)
{
std::vector<std::string> vec;
int i, j;
for (i = 0, j = 1; j < str.size(); j++)
{
if (str[j] == sep)
{
if (i == j)
vec.push_back("");
else
vec.push_back(str.substr(i, j - i));
i = j + 1;
}
}
if (j + 1 != i)
vec.push_back(str.substr(i, j - 1));
return vec;
}
std::vector<std::string> StringUtil::splitClean(std::string str, std::string sep)
{
std::vector<std::string> vec;
while (str.size() != 0)
{
size_t index = str.find(sep);
if (index)
vec.push_back(str.substr(0, index));
str = str.substr(index + sep.size());
}
return vec;
}
std::vector<std::string> StringUtil::splitClean(std::string str, char sep)
{
std::vector<std::string> vec;
int i, j;
for (i = 0, j = 1; j < str.size(); j++)
{
if (str[j] == sep && i != j)
{
vec.push_back(str.substr(i, j - i));
i = j + 1;
}
}
if (j + 1 != i)
vec.push_back(str.substr(i, j - 1));
return vec;
}
std::string StringUtil::strip(std::string str, char sep)
{
return stripLeft(stripRight(str, sep), sep);
}
std::string StringUtil::stripLeft(std::string str, char sep)
{
int n;
for (n = 0; n < str.size(); n++)
if (str[n] != sep)
break;
return str.substr(n);
}
std::string StringUtil::stripRight(std::string str, char sep)
{
int n;
for (n = (int)str.size()-1; n > -1; n--)
if (str[n] != sep)
break;
return str.substr(0, n+1);
}