C++ 提取字符串中的所有中文

 例:

   啊12你好5哈哈3

结果:

啊 你好 哈哈

上代码

#include<iostream>
#include<list>
#include<string>
#include<map>
#include<vector>

using namespace std;


list<string> GetStringByStringArr(string str);

 int main() {
	 
	 string str;
	 map<string, float> tempmap;
	 while (true)
	 {
		 cout << "请输入数据" << endl;
		 cin >> str;

		 if (str == "Q")
		 {
			 break;
		 }

		 GetStringByStringArr(str);

		 str.clear();
	 }
	 
	 system("pause");

	 return 0;
}


 list<string> GetStringByStringArr(string str) {

	 list<string> strlist;

	 vector<char> tempArr;

	 for (size_t i = 0; i < str.length(); i++)
	 {
		 if ((str[i] & 0x80) != 0)		
		 {
			 tempArr.push_back(str[i]);
		 }
		 else
		 {
			 if (tempArr.empty())
			 {
				 continue;
			 }
			 else
			 {
				 strlist.push_back(string(tempArr.begin(), tempArr.end()));
				 cout << string(tempArr.begin(), tempArr.end()) << endl;
				 tempArr.clear();
			 }
		 }
	 }

	 if (!tempArr.empty())		//防止在结尾的汉字不被添加
		 strlist.push_back(string(tempArr.begin(), tempArr.end()));

	 return strlist;
 }

猜你喜欢

转载自blog.csdn.net/qq_39342142/article/details/83379273