// 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

c++拆分字符,不拆开中文的更多相关文章

  1. C#实现判断字符是否为中文

    C#实现判断字符是否为中文 (2012-08-14 14:25:28) 标签: gb2312 big5编码 gbk编码 判断 汉字 杂谈 分类: 技术 protected bool IsChinese ...

  2. debian8最小化安装,字符界面的中文配置

    一.现象: debian8最小化安装以后,字符界面的中文显示乱码. 二.解决 1. 安装locales apt-get install locales 2. 配置locales dpkg-reconf ...

  3. Android中判断字符是否为中文、韩文、日文

    我们经常需要在程序中判断一个字符是否为CJK(Chinese.Japanese.Korean)语言的字符. 例如,在Contacts里面程序需要判断联系人姓名的所属语言. 今天为大家介绍一种NameS ...

  4. java面试题:如果一串字符如"aaaabbc中国1512"要分别统计英文字符的数量,中文字符的数量,和数字字符的数量,假设字符中没有中文字符、英文字符、数字字符之外的其他特殊字符。

    package com.swift; public class TotalNumber_String { public static void main(String[] args) { /* * 如 ...

  5. 编码对象或者字串中包含Unicode字符怎样转换为中文

    In [18]: c = '你好' In [20]: d = c.encode('unicode_escape') In [21]: d Out[21]: b'\\u4f60\\u597d' In [ ...

  6. JavaScript判断字符串的字符长度(中文占两个字符)

    判断方法 //判断字符串中的字符 中文算两个字符 function chkstrlen(str) { ; ; i < str.length; i++) { ) //如果是汉字,则字符串长度加2 ...

  7. HTML基础之JS中的字符转义--转义中文或特殊字符

    1.在标准的url的规范中是不允许出现中文字符或某些特殊字符的,所以要进行转义 2.& 代表参数的链接,如果就是想传& 给后端那么必须转义 decodeURI(url) URl中未转义 ...

  8. 限制input输入字符数(中文2个字符,英文1个字符)

    input的maxlength可以限制input的输入的字符数,但是是字符串的长度,相当于判断str.length;然而经常会有中文字符算2个字符英文算1个字符的需求,目前只能通过编写代码来实现. & ...

  9. vue, js 正则邮箱验证、匹配非法字符、匹配中文

    验证邮箱 let self = this let regEmail= /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)* ...

随机推荐

  1. (MUA)mutt-"No authenticators available" 发送不出去邮件

    问题: 使用mutt发送邮件的时候,在调试模式下总是显示"No authenticators available", 后来在google上查到可能要设置验证方法gssapi,但是设 ...

  2. 4.GlusterFS 常见故障处理

    一般硬盘也要备用几块,因为随着发展,可能这些型号的硬盘不好买到了,一般的事故不会在开始一两年出,在硬件老化的时候出故障的频率高. 4.1 硬盘故障 如果底层做了 RAID 配置,有硬件故障,直接更换硬 ...

  3. August 02nd 2017 Week 31st Wednesday

    Love means never having to say you are sorry. 爱就是永远不必说对不起. If there is ture love, you will never do ...

  4. Server Host Cannot be null解决方法

    在用打开Services Directory application 或者访问 某个已发布的地图服务时,出现"Server Host Cannot be null"的错误. 问题的 ...

  5. Centos7 之目录处理命令(八)

    linux中 关于目录 有几个重要概念 一个是 / 根目录  还有一个当前用户的家目录 比如 root用户的家目录是 /root  普通用户的家目录是/home/xxx 下 root登录 默认家目录 ...

  6. Calabash(葫芦娃)

    嘟嘟嘟 第一眼就觉得肯定某种是最短路,然后想了半天也不知道.然后就把送的50分写了,然后就爆搜,结果因为一个错误的剪枝竟然90分?!只能怪数据太水…… 考完试后听bin哥讲,说就是普通的最短路,只不过 ...

  7. 【JavaScript】explode动画

    这是一个js实现的粒子聚合文字或图片的动画特效 部分程序如下 n.container = n.container[0] || n.container; /*有且仅有一个container*/ var ...

  8. PAT——1020. 月饼

    月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼.现给定所有种类月饼的库存量.总售价.以及市场的最大需求量,请你计算可以获得的最大收益是多少. 注意:销售时允许取出一部分库存.样 ...

  9. [LuoguP1064][Noip2006]金明的预算方案

    金明的预算方案(Link) 题目描述 现在有\(M\)个物品,每一个物品有一个钱数和重要度,并且有一个\(Q\),如果\(Q = 0\),那么该物件可以单独购买,当\(Q != 0\)时,表示若要购买 ...

  10. js尾巴

    js中根据id获取标签: /** * 根据id获取标签 * @param {string}id * @returns {object} */ function $(id) { return typeo ...