// 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. Python学习---Model拾遗[2]180318

    Model的字段及字段参数: Model字段: 数字        字符串(带正则的字段)        时间        文件       特殊字段:(一对一,一对多,多对多) Models.py ...

  2. python取当前时间前后一定间隔的时间点

    当前时间 datetime.datetime.now() 时间间隔 datetime.timedelta(参数=数值) #参数:weeks,days,hours,minutes,seconds,mic ...

  3. CentOS7.X安装Redis-4.0.8以及Redis集群搭建

    安装redis 安装前的准备 yum install \ vim \ wget \ make \ gcc \ gcc-c++ \ automake \ autoconf \ -y \ 下载解压并安装 ...

  4. Failed to read Class-Path attribute from manifest of jar file:/XXX问题

    java.lang.IllegalStateException: Failed to read Class-Path attribute from manifest of jar file:/XXX ...

  5. 协议森林03 IP接力赛 (IP, ARP, RIP和BGP协议)

    网络层(network layer)是实现互联网的最重要的一层.正是在网络层面上,各个局域网根据IP协议相互连接,最终构成覆盖全球的Internet.更高层的协议,无论是TCP还是UDP,必须通过网络 ...

  6. 2、Web Service-术语

    1.Java中的Web Service规范 三种规范:JAXM&SAAJ.JAX-WS(JAX-RPC).JAX-RS. 三要素:soap,wsdl,uddi 1. Jaxws(掌握) JAX ...

  7. Vue系列——在vue项目中使用echarts

    该示例使用 vue-cli 脚手架搭建 安装echarts依赖 npm install echarts -S 或者使用国内的淘宝镜像安装 npm install -g cnpm --registry= ...

  8. xmppframework 简介

    XMPPFramework是一个OS X/iOS平台的开源项目,使用Objective-C实现了XMPP协议(RFC-3920),同时还提供了用于读写XML的工具,大大简化了基于XMPP的通信应用的开 ...

  9. Spring+Hibernate+struts2+JPA 注解+跨域//完成手机端点击加载更多 下拉加载更多

    一.使用IDEA新建一个maven项目(student) 1.1.0编写pom文件,添加项目所需要的包 <?xml version="1.0" encoding=" ...

  10. 怎样实现一个简单的jQuery编程

    第一步:在head中载入jQuery框架 <script type="text/javascript"  src="jQuery文档所在的绝对路径"> ...