目录
三、ANSI,Unicode,utf-8,string之间的转换
一、替换Replace
std::string Replace(const std::string& strSrc, const std::string& strRaw, const std::string& strReplace)
{
if (strSrc.empty())
{
return strSrc;
}
std::string strRet;
std::string strToBeAnalysis = strSrc;
do
{
int nPos = strToBeAnalysis.find(strRaw);
if (nPos == std::string::npos)
{
strRet += strToBeAnalysis;
break;
}
strRet += strToBeAnalysis.substr(0, nPos);
strRet += strReplace;
strToBeAnalysis = strToBeAnalysis.substr(nPos + strRaw.length());
} while (true);
return strRet;
}
将逗号替换成分号
string str = "123,456,789,";
string strRet = Replace(str, ",", ";");//strRet 123;456;789
二、分割Split
std::vector<std::string> Split(const std::string &str, const std::string &delim)
{
size_t pos, last_pos = 0, len;
std::vector<std::string> tokens;
while (true)
{
pos = str.find(delim, last_pos);
if (pos == std::string::npos)
{
pos = str.size();
}
len = pos - last_pos;
if (len != 0)
{
tokens.push_back(str.substr(last_pos, len));
}
if (pos == str.size())
{
break;
}
else
{
last_pos = pos + delim.size();
}
}
return tokens;
}
以逗号分割
std::string str = "123,456,789,";
std::vector<std::string> vecStr;
vecStr = Split(str, ",");
三、ANSI,Unicode,utf-8,string之间的转换
详见:字符编码问题
std::wstring ANSI2Unicode(LPCSTR lpszSrc)
{
std::wstring strResult;
if (lpszSrc != NULL)
{
//1.先拿到转换后的宽字符的大小nUnicodeLen
int nUnicodeLen = MultiByteToWideChar(CP_ACP, 0, lpszSrc, -1, NULL, 0);
wchar_t *pUnicode = new wchar_t[nUnicodeLen];
if (pUnicode != NULL)
{
memset(pUnicode, 0, nUnicodeLen * sizeof(wchar_t));
//2.转换
int n = MultiByteToWideChar(CP_ACP, 0, lpszSrc, -1, pUnicode, nUnicodeLen);
strResult = pUnicode;
delete[] pUnicode;
pUnicode = NULL;
}
}
return strResult;
}
std::string Unicode2ANSI(LPCWSTRlpszSrc)
{
std::string strResult;
if (lpszSrc != NULL)
{
int nANSILen = WideCharToMultiByte(CP_ACP, 0, lpszSrc, -1, NULL, 0, NULL, NULL);
char * pANSI = new char[nANSILen];
if (pANSI != NULL)
{
memset(pANSI, 0, nANSILen);
WideCharToMultiByte(CP_ACP, 0, lpszSrc, -1, pANSI, nANSILen, NULL, NULL);
strResult = pANSI;
delete[] pANSI;
pANSI = NULL;
}
}
return strResult;
}
std::string Unicode2UTF8(LPCWSTR lpszSrc)
{
std::string strResult;
if (lpszSrc != NULL)
{
int nUTF8Len = WideCharToMultiByte(CP_UTF8, 0, lpszSrc, -1, NULL, 0, NULL, NULL);
char * pUTF8 = new char[nUTF8Len];
if (pUTF8 != NULL)
{
memset(pUTF8, 0, nUTF8Len);
WideCharToMultiByte(CP_UTF8, 0, lpszSrc, -1, pUTF8, nUTF8Len, NULL, NULL);
strResult = pUTF8;
delete[] pUTF8;
pUTF8 = NULL;
}
}
return strResult;
}
std::wstring UTF82Unicode(const std::string &utf8_str)
{
std::wstring strResult;
int nUnicodeLen = MultiByteToWideChar(CP_UTF8, 0, utf8_str.c_str(), -1, NULL, 0);
wchar_t *pUnicode = new wchar_t[nUnicodeLen];
if (pUnicode != NULL)
{
memset(pUnicode, 0, nUnicodeLen * sizeof(wchar_t));
MultiByteToWideChar(CP_UTF8, 0, utf8_str.c_str(), -1, pUnicode, nUnicodeLen);
strResult = pUnicode;
delete[] pUnicode;
pUnicode = NULL;
}
return strResult;
}
std::string string2UTF8(const std::string & str)
{
std::string strResult;
//1.string 转unicode
int nUnicodeLen = MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);
wchar_t *pUnicode = new wchar_t[nUnicodeLen];
if (pUnicode != NULL)
{
memset(pUnicode, 0, nUnicodeLen * sizeof(wchar_t));
MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, pUnicode, nUnicodeLen);
//2.unicode转utf8
int nUTF8Len = WideCharToMultiByte(CP_UTF8, 0, pUnicode, -1, NULL, 0, NULL, NULL);
char *pUTF8 = new char[nUTF8Len];
if (pUTF8 != NULL)
{
memset(pUTF8, 0, nUTF8Len);
WideCharToMultiByte(CP_UTF8, 0, pUnicode, -1, pUTF8, nUTF8Len, NULL, NULL);
strResult = pUTF8;
delete[] pUTF8;
pUTF8 = NULL;
}
delete[] pUnicode;
pUnicode = NULL;
}
return strResult;
}
std::string UTF82string(const std::string & str)
{
std::string strResult;
//1.utf8转unicode
int nUnicodeLen = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);
wchar_t *pUnicode = new wchar_t[nUnicodeLen];
if (pUnicode != NULL)
{
memset(pUnicode, 0, nUnicodeLen * sizeof(wchar_t));
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, pUnicode, nUnicodeLen);
//2.unicode转string
int nLen = WideCharToMultiByte(CP_ACP, 0, pUnicode, -1, NULL, 0, NULL, NULL);
char *pANSI = new char[nLen];
if (pANSI != NULL)
{
memset(pANSI, 0, nLen);
WideCharToMultiByte(CP_ACP, 0, pUnicode, -1, pANSI, nLen, NULL, NULL);
strResult = pANSI;
delete[] pANSI;
pANSI = NULL;
}
delete[] pUnicode;
pUnicode = NULL;
}
return strResult;
}
代码举例:
misc_util.h
#ifndef MISC_UTIL_H_
#define MISC_UTIL_H_
#include <iostream>
#include <vector>
namespace miscutil
{
namespace convert
{
std::string Replace(const std::string& strSrc, const std::string& strRaw, const std::string& strReplace);
std::vector<std::string> Split(const std::string &str, const std::string &delim);
}
}
#endif
misc_util.cpp
#include "misc_util.h"
namespace miscutil
{
namespace convert
{
std::string Replace(const std::string& strSrc, const std::string& strRaw, const std::string& strReplace)
{
if (strSrc.empty())
{
return strSrc;
}
std::string strRet;
std::string strToBeAnalysis = strSrc;
do
{
int nPos = strToBeAnalysis.find(strRaw);
if (nPos == std::string::npos)
{
strRet += strToBeAnalysis;
break;
}
strRet += strToBeAnalysis.substr(0, nPos);
strRet += strReplace;
strToBeAnalysis = strToBeAnalysis.substr(nPos + strRaw.length());
} while (true);
return strRet;
}
std::vector<std::string> Split(const std::string &str, const std::string &delim)
{
size_t pos, last_pos = 0, len;
std::vector<std::string> tokens;
while (true)
{
pos = str.find(delim, last_pos);
if (pos == std::string::npos)
{
pos = str.size();
}
len = pos - last_pos;
if (len != 0)
{
tokens.push_back(str.substr(last_pos, len));
}
if (pos == str.size())
{
break;
}
else
{
last_pos = pos + delim.size();
}
}
return tokens;
}
}
}
main函数
#include "misc_util.h"
int main()
{
std::string str = "123,456,789,";
std::vector<std::string> vecStr;
vecStr = miscutil::convert::Split(str, ",");
return 0;
}