C++11去除空格或者无效字符\
std::string& trim(std::string& s, const std::string& c = "\x20")
{
if (!s.empty())
{
s.erase(0, s.find_first_not_of(c));
s.erase(s.find_last_not_of(c) + 1);
}
return s;
}
ANSI、UTF8、UNICODE转换\快速分割字符串
#include <regex>
std::wstring UTF8_2_W(const std::string& s)
{
std::wstring ws(::MultiByteToWideChar(CP_UTF8, 0, s.data(), -1, NULL, 0), L'\0');
::MultiByteToWideChar(CP_UTF8, 0, s.data(), -1, (LPWSTR)ws.data(), (INT)ws.size());
return ws;
}
std::string W_2_UTF8(const std::wstring& ws)
{
std::string s(::WideCharToMultiByte(CP_UTF8, 0, ws.data(), -1, NULL, 0, NULL, NULL), '\0');
::WideCharToMultiByte(CP_UTF8, 0, ws.data(), -1, (LPSTR)s.data(), (INT)s.size(), NULL, NULL);
return s;
}
std::wstring A_2_W(const std::string& s)
{
std::wstring ws(::MultiByteToWideChar(CP_ACP, 0, s.data(), -1, NULL, 0), L'\0');
::MultiByteToWideChar(CP_ACP, 0, s.data(), -1, (LPWSTR)ws.data(), (INT)ws.size());
return ws;
}
std::string W_2_A(const std::wstring& ws)
{
std::string s(::WideCharToMultiByte(CP_ACP, 0, ws.data(), -1, NULL, 0, NULL, NULL), '\0');
::WideCharToMultiByte(CP_ACP, 0, ws.data(), -1, (LPSTR)s.data(), (INT)s.size(), NULL, NULL);
return s;
}
std::string UTF8_2_A(const std::string& s)
{
return W_2_A(UTF8_2_W(s));
}
std::string A_2_UTF8(const std::string& s)
{
return W_2_UTF8(A_2_W(s));
}
std::vector<std::string> string_split(const std::string& s, const std::string& delim) {
std::regex re{ delim };
return std::vector<std::string>{std::sregex_token_iterator(s.begin(), s.end(), re, -1), std::sregex_token_iterator()};
}
使用例子:
std::vector<std::string> splitteVersion = string_split("1.2.3.4", ("[\\s.?]+"));