// 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. 第三次作业 史浩然 -assassin Talon

  2. xsd文件记录

    <MESSAGE Version="1.0"> <CV_HEADER MsgType=" /> <QUERY_PROFILE> < ...

  3. 四、C# 5.0 新特性——Async和Await使异步编程更简单

    一.引言 .NET 4.5 的推出,对于C#又有了新特性的增加--就是C#5.0中async和await两个关键字,这两个关键字简化了异步编程,之所以简化了,还是因为编译器给我们做了更多的工作,下面就 ...

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

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

  5. UIView 中 hidden、alpha、clear color 与 opaque 的区别

    透明度与图层混合相关,影响到图片绘制的效率. hidden 此属性为 BOOL 值,用来表示 UIView 是否隐藏.关于隐藏大家都知道就是让 UIView 不显示而已,但是需要注意的是: 当前 UI ...

  6. eclipse 自动生成get/set方法

    Shift+Alt+S 会弹出一个对话框 选择Generate Getters and Setters

  7. [转]谈谈关于MVP模式中V-P交互问题

    在差不多两年的时间内,我们项目组几十来号人都扑在一个项目上面.这是一个基于微软SCSF(Smart Client Software Factory)的项目,客户端是墨尔本一家事业单位.前两周,我奉命负 ...

  8. nginx判断为404跳转

    server { listen 80; server_name localhost www.beautysaas.com 120.26.126.123; error_page 404 = http:/ ...

  9. 【转】上传jar包到nexus私服

    原文:https://my.oschina.net/lujianing/blog/297128 1通过网页上传 这种方法只是上传了jar包.通过maven引用当前jar,不能取得jar的依赖 from ...

  10. 跳转到系统设置界面 iOS

    NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; if ([[UIApplication sharedApp ...