工具类总结

 

 

 

目录

一、替换Replace

二、分割Split

三、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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值