feat add StrSpilt,Trim,TrimLeft,TrimRight
This commit is contained in:
parent
3dccac62b7
commit
abc1a88bd0
@ -1,6 +1,8 @@
|
|||||||
|
#pragma once
|
||||||
#ifndef SLED_STRINGS_UTILS_H
|
#ifndef SLED_STRINGS_UTILS_H
|
||||||
#define SLED_STRINGS_UTILS_H
|
#define SLED_STRINGS_UTILS_H
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
namespace sled {
|
namespace sled {
|
||||||
|
|
||||||
@ -10,6 +12,14 @@ std::string ToHex(const std::string &str);
|
|||||||
std::string StrJoin(const std::vector<std::string> &strings,
|
std::string StrJoin(const std::vector<std::string> &strings,
|
||||||
const std::string &delim,
|
const std::string &delim,
|
||||||
bool skip_empty = false);
|
bool skip_empty = false);
|
||||||
|
std::vector<std::string> StrSplit(const std::string &str,
|
||||||
|
const std::string &delim,
|
||||||
|
bool skip_empty = false);
|
||||||
|
std::string Trim(const std::string &str, const std::string &chars = " \t\n\r");
|
||||||
|
std::string TrimLeft(const std::string &str,
|
||||||
|
const std::string &chars = " \t\n\r");
|
||||||
|
std::string TrimRight(const std::string &str,
|
||||||
|
const std::string &chars = " \t\n\r");
|
||||||
|
|
||||||
}// namespace sled
|
}// namespace sled
|
||||||
#endif// SLED_STRINGS_UTILS_H
|
#endif// SLED_STRINGS_UTILS_H
|
||||||
|
@ -20,4 +20,49 @@ StrJoin(const std::vector<std::string> &strings,
|
|||||||
}
|
}
|
||||||
return ss.str();
|
return ss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<std::string>
|
||||||
|
StrSplit(const std::string &str, const std::string &delim, bool skip_empty)
|
||||||
|
{
|
||||||
|
std::vector<std::string> result;
|
||||||
|
if (str.empty()) { return result; }
|
||||||
|
|
||||||
|
size_t start = 0;
|
||||||
|
size_t next_pos = str.find_first_of(delim, start);
|
||||||
|
while (next_pos != std::string::npos) {
|
||||||
|
if (!skip_empty || next_pos > start) {
|
||||||
|
result.emplace_back(str.substr(start, next_pos - start));
|
||||||
|
}
|
||||||
|
start = next_pos + 1;
|
||||||
|
next_pos = str.find_first_of(delim, start);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (start < str.size()) { result.emplace_back(str.substr(start)); }
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string
|
||||||
|
Trim(const std::string &str, const std::string &chars)
|
||||||
|
{
|
||||||
|
return TrimLeft(TrimRight(str));
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string
|
||||||
|
TrimLeft(const std::string &str, const std::string &chars)
|
||||||
|
{
|
||||||
|
size_t start = 0;
|
||||||
|
while (start < str.size() && chars.find(str[start]) != std::string::npos) {
|
||||||
|
++start;
|
||||||
|
}
|
||||||
|
return str.substr(start);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string
|
||||||
|
TrimRight(const std::string &str, const std::string &chars)
|
||||||
|
{
|
||||||
|
size_t end = str.size();
|
||||||
|
while (end > 0 && chars.find(str[end - 1]) != std::string::npos) { --end; }
|
||||||
|
return str.substr(0, end);
|
||||||
|
}
|
||||||
|
|
||||||
}// namespace sled
|
}// namespace sled
|
||||||
|
Loading…
Reference in New Issue
Block a user