C/C++简易分割字符串示例

此方法并不完美,仅供参考

#include <windows.h>
#include <stdio.h>
#include <iostream>
#include <string>

using namespace std;

string CutText(string &text,size_t cutLimitlen)
{
	string retStr = "";
	int retStrSize = 0;
	if(text.size() <= cutLimitlen)
	{
		retStr = text;
		text.clear();
		return retStr;
	}
	for(int i = 0; i < text.size(); i++)
	{
		if (text[i] & 0x80) //汉字
		{
			retStr += text[i];
			retStr += text[i+1];
			retStrSize += 2;
			i++;
		}
		else
		{
			retStr += text[i];
			retStrSize += 1;
		}
		if(retStrSize >= cutLimitlen)
		{
			text=text.substr(retStr.length(),text.length()-retStr.length());
			break;
		}
	}
	return retStr;
}
void main()
{
	string strText = "[视野]我自己:012/*-=+3qwe4asd5abcd67哈▲※★ㄊㄍㄐちたそ┯┷喽8凤姐bb无敌9)(*&^%ャモメ∶!】贰零壹零⑹⑻ωψχāáé";
	static bool bF = true;
	while (!strText.empty())
	{
		string strShowText;
		if(bF)
		{
			strShowText = CutText(strText,38);
			bF = false;
		}
		else
		{
			strShowText = CutText(strText,32);
			strShowText = "      " + strShowText;
		}
		cout<<strShowText<<endl;
	}
	system("pause");
}

运行结果:

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页