C++ 截取宽字符串示例

这篇博客介绍了如何在C++中截取宽字符串,提供了包含头文件和实例代码的详细步骤,帮助读者理解宽字符串操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

宽字符串示例:

头文件:

void WStringSplit( const std::wstring & wstr, std::vector<std::wstring> & tokens, const std::wstring & delimiters);

实现文件:

void WStringSplit( const std::wstring & wstr, vector<std::wstring> & tokens, const std::wstring & delimiters)
{
	// Skip delimiters at beginning.
	std::wstring::size_type lastPos = wstr.find_first_not_of(delimiters, 0);
	// Find first "non-delimiter".
	std::wstring::size_type pos = wstr.find_first_of(delimiters, lastPos);
	while (std::wstring::npos != pos || std::wstring::npos != lastPos)
	{
		// Found a token, add it to the vector.
		tokens.push_back(wstr.substr(lastPos, pos - lastPos));
		// Skip delimiters. 
		lastPos = wstr.find_first_not_of(delimiters, pos);
		// Find next "non-delimiter"
		pos = wstr.find_first_of(delimiters, lastPos);
	}
}

宽字符使用方式:

			std::vector<std::wstring> tokens;
			std::wstring urlstr(buffer); 
			std::wstring delimiter(L"&");
			std::wstring tempstr;
			WStringSplit(urlstr, tokens, delimiter);


普通字符串示例:

头文件:

void StringSplit( const std::string & str, std::vector<std::string> & tokens, const std::string & delimiters);

实现文件:

void StringSplit( const std::string & str, vector<std::string> & tokens, const std::string & delimiters)
{
	// Skip delimiters at beginning.
	std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
	// Find first "non-delimiter".
	std::string::size_type pos = str.find_first_of(delimiters, lastPos);
	while (std::string::npos != pos || std::string::npos != lastPos)
	{
		// Found a token, add it to the vector.
		tokens.push_back(str.substr(lastPos, pos - lastPos));
		// Skip delimiters. 
		lastPos = str.find_first_not_of(delimiters, pos);
		// Find next "non-delimiter"
		pos = str.find_first_of(delimiters, lastPos);
	}
}

使用方式:

vector<string> tmp;
StringSplit(str, tmp, ",");
int size = tmp.size();







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值