// 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. Java学习---异常处理的学习

    基础知识 任何一门计算机程序设计语言都包括有绝对正确和相对正确的语句.绝对正确: 指任何情况下, 程序都会按照流程正确执行:相对正确: 程序的运行受到运行环境的制约, 在这种情况下, 需要附加检测和控 ...

  2. php使用<?php include之后页首有空白

    1.今天做这一个页面的时候发现如果使用<?php include包含了一个页面,但是页首始终有一个空白行,度娘出来原来是因为使用了UTF-8格式,应该选择UFT-8无BOM格式的,原来还有这一说 ...

  3. Linux命令--目录处理

    ls命令 Linux ls命令用于显示指定工作目录下之内容(列出目前工作目录所含之文件及子目录). 语法 ls [-alrtAFR] [name...] 参数 : -a 显示所有文件及目录 (ls内定 ...

  4. 判断元素(expected_conditions)

    判断元素 如何判断一个元素是否存在,如何判断 alert 弹窗出来了,如何判断动态的元素等等一系列的判断,在 selenium 的 expected_conditions 模块收集了一系列的场景判断方 ...

  5. java冒泡排序-选择排序-插入排序-使用API中文文档直接调用函数

    import java.util.Arrays; public class ArrayDemo2_3 { public static void main(String []args) { //---- ...

  6. jq局部打印插件jQuery.print.js(兼容IE8)

    /* @license * jQuery.print, version 1.5.1 * (c) Sathvik Ponangi, Doers' Guild * Licence: CC-By (http ...

  7. The Struts dispatcher cannot be found. This is usually caused by using Struts

    对于struts2中的问题: org.apache.jasper.JasperException: The Struts dispatcher cannot be found. This is usu ...

  8. POJ2724 Purifying Machine

    嘟嘟嘟 扒下来的题意:迈克有一台可以净化奶酪的机器,用二进制表示净化的奶酪的编号.但是,在某些二进制串中可能包含有\(*\).例如\(01*100\),\(*\)其实就代表可以取\(0\),\(1\) ...

  9. Odoo中的向导

    转载请注明原文地址:https://www.cnblogs.com/cnodoo/p/9281320.html 一:向导及其效果 向导类似于弹窗,用于接收用户的输入,然后作出相应的处理. 二:定义向导 ...

  10. 9、RabbitMQ-集成Spring

    spring封装RabbitMQ看官网:https://spring.io/projects/spring-amqp#learn 开发时根据官网的介绍进行开发,具体的说明都有具体的声明 1.导入依赖 ...