c++拆分字符,不拆开中文

// ConsoleApplication2.cpp : 定义控制台应用程序的入口点。
//
 
#include "stdafx.h"
#include <string.h>
#include <string>
#include <vector>
#include <iostream>
 
using namespace std;
 
 
std::wstring GBKToUnicode(const std::string& src)
{
	setlocale(LC_ALL, "chs");// 设置为中文环境,不然可能会转换失败
	std::vector<wchar_t> dst(src.size() + 1, L'\0');
	size_t count = std::mbstowcs(&dst[0], src.c_str(), dst.size() - 1);
	setlocale(LC_ALL, "C");
	return std::wstring(&dst[0]);
}
 
 
// 判断字符串是否有中文
bool hasChinese(const std::string& src, int* array)
{
	auto w = GBKToUnicode(src);
	int i = 0;
	for (auto c : w)
	{
		if (c >= 0x4E00 && c <= 0x9FCB || c >= 0x3400 && c <= 0x4DB5)
		{
			array[i++] = 1;
			//i++;	// 向后延一位
		}
 
		i++;
	}
 
	return false;
}
 
 
int splitText(char* text, int len)
{
	if (NULL == text)
	{
		return 0;
	}
	
	int arr[1024]; memset(arr, 0x00, sizeof(arr));
	hasChinese(text, arr);
	char temp[1024]; memset(temp, 0x00, sizeof(temp));
	int text_len = (int)strlen(text);
	int j = 0;
	for (int i = 0; i < text_len; i++)
	{
		if (j >= len)
		{
			if (arr[i-1] == 1)
			{
				temp[j++] = text[i++];
			}
 
			printf("%s\n", temp);
			j = 0;
			memset(temp, 0x00, sizeof(temp));
		}
 
		temp[j++] = text[i];
 
	}
 
	printf("%s\n", temp);
 
 
	return 0;
}
 
 
 
int main(){
 
//	wcout << hasChinese("中国123他们是谁21423") << endl;
	splitText("中国123他们是谁21423", 5);
	return 0;
}
 
 
 
 
 
 
 
 中文的ascii为负数
//int splitText(char* text, int len)
//{
//	if (NULL == text)
//	{
//		return 0;
//	}
//
//
//	char text_temp[1024]; memset(text_temp, 0x00, sizeof(text_temp));
//	strcpy_s(text_temp, text);
//
//	int text_len = (int)strlen(text);
//	int j = 0;
//	char temp[1024]; memset(temp, 0x00, sizeof(temp));
//	for (int i = 0; i < text_len; i++)
//	{
//		if (j >= len)
//		{
//			if ((text[i - 1] & 0x80) == 0) {   //ascii begin with 0 
//
//			}
//			else
//			{
//				如果字符高位为1且下一字符高位也是1则有中文字符
//				//if ((text[i - 1] & 0x80) && (text[i] & 0x80))
//				unsigned char text_i_1 = (unsigned char)text[i - 1];
//				unsigned char text_i = (unsigned char)text[i];
//
//				int high = text_i << 8;
//
//
//				int c = high + text_i_1;
//
//
///*				if (((text_i_1 >= 0) && (text_i_1 <= 0xCB) && (text_i >= 0x4E) && (text_i <= 0x9F)) ||
//					((text_i_1 >= 0) && (text_i_1 <= 0xB5) && (text_i >= 0x34) && (text_i <= 0x4D)))*/
//				if (c >= 0x4E00 && c <= 0x9FCB
//					|| c >= 0x3400 && c <= 0x4DB5)
//				{
//					temp[j] = text[i++];
//				}
//			}
//
//
//			printf("%s\n", temp);
//			j = 0;
//			memset(temp, 0x00, sizeof(temp));
//			//continue;
//		}
//
//		temp[j++] = text[i];
//		printf("%d %s\n", i, temp);
//
//	}
//	printf("%s\n", temp);
//	return 0;
//}
 

参考网站:https://www.zhihu.com/question/57479676/answer/153052641

转载于:https://www.cnblogs.com/20170722-kong/p/8906905.html

<> 一些背景知识: 1. 一个汉字在c\c++的存储, 使用2个字节(char)存储; 2. 汉字存储的第一个char, 其值一定大于'~'(0111 1110=126),否则将导致识别歧义; 此处, 使用"单ASCII字符"来表示非 单ASCII字符的判断, 可以根据以下规则(主要R3, R4); : 一个字符串的长度==1,那肯定是一个ASCII字符; : 一个字符串的长度==2,且第1个ASCII字符的编码小于'~'; 那肯定是2个单ASCII字符; -- (PS: "") : 一个字符串的长度==3,且中间的ASCII字符的编码小于'~'; 那第3个肯定是单ASCII字符; (前2个是否构成1个汉字不能确定) : (基于R3)如果第pos个位置的ASCII字符编码小于'~', 那从下标pos,pos+1处拆分字符串 (下标pos归前串),将不会导致乱码; A 或者 B 的选择,需要知道以下信息: (1). 从 cut_base_pos 开始到 pos_B 结束的这段字符串内, 最后一个单ASCII字符的结束下标 p 在此基础上, 若使用: y -- 表示单ASCII字符(已确定的); x -- 表示可能是 单ASCII字符,也可能是 汉字的半个ASCII存储码; 那么一段长度为N的字符串,按照存储的ASCII码可以表示为一串如下的字符串(不包含[]) "x .. x][x .. x y x .. x][x .. x", 其中 s,e表示当前正在分析的一段子串(0<=s<e<=N, 下标e-s = 期望的分割长度cut_size) ^ ^ ^ ^ ^ 0 s p e N 在 【s, e】 之间, 查找结束下标p 的思路: 从e开始向s查找, 找到 第一个y 后 break; 记录下标p, 则从位置p开始, 是一个正确的分割(cut); 但此分割并不是一个最好的分割. 可以在 位置p上, 再加上 2K 个长度, 使 p+2K 与 pos_B 最接近即可. 可以认为从2K个长度的内容是K个汉字(实际上并不一定..), 但并不影响这个最佳分割的正确性! :)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值