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
c++拆分字符,不拆开中文的更多相关文章
- C#实现判断字符是否为中文
C#实现判断字符是否为中文 (2012-08-14 14:25:28) 标签: gb2312 big5编码 gbk编码 判断 汉字 杂谈 分类: 技术 protected bool IsChinese ...
- debian8最小化安装,字符界面的中文配置
一.现象: debian8最小化安装以后,字符界面的中文显示乱码. 二.解决 1. 安装locales apt-get install locales 2. 配置locales dpkg-reconf ...
- Android中判断字符是否为中文、韩文、日文
我们经常需要在程序中判断一个字符是否为CJK(Chinese.Japanese.Korean)语言的字符. 例如,在Contacts里面程序需要判断联系人姓名的所属语言. 今天为大家介绍一种NameS ...
- java面试题:如果一串字符如"aaaabbc中国1512"要分别统计英文字符的数量,中文字符的数量,和数字字符的数量,假设字符中没有中文字符、英文字符、数字字符之外的其他特殊字符。
package com.swift; public class TotalNumber_String { public static void main(String[] args) { /* * 如 ...
- 编码对象或者字串中包含Unicode字符怎样转换为中文
In [18]: c = '你好' In [20]: d = c.encode('unicode_escape') In [21]: d Out[21]: b'\\u4f60\\u597d' In [ ...
- JavaScript判断字符串的字符长度(中文占两个字符)
判断方法 //判断字符串中的字符 中文算两个字符 function chkstrlen(str) { ; ; i < str.length; i++) { ) //如果是汉字,则字符串长度加2 ...
- HTML基础之JS中的字符转义--转义中文或特殊字符
1.在标准的url的规范中是不允许出现中文字符或某些特殊字符的,所以要进行转义 2.& 代表参数的链接,如果就是想传& 给后端那么必须转义 decodeURI(url) URl中未转义 ...
- 限制input输入字符数(中文2个字符,英文1个字符)
input的maxlength可以限制input的输入的字符数,但是是字符串的长度,相当于判断str.length;然而经常会有中文字符算2个字符英文算1个字符的需求,目前只能通过编写代码来实现. & ...
- vue, js 正则邮箱验证、匹配非法字符、匹配中文
验证邮箱 let self = this let regEmail= /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)* ...
随机推荐
- VSTO 开发
http://www.cnblogs.com/yangecnu/category/499866.html http://www.cnblogs.com/brooks-dotnet/category/2 ...
- Python学习---抽屉框架分析[数据库设计分析]180313
基本的: models.py ####################################以下都是抽屉的代码#################################### fro ...
- January 07 2017 Week 1st Saturday
Procrastination is the thief of time. 拖延乃是光阴之窃贼. My parents always tell me that things ought to be d ...
- Scala隐式转换和隐式参数
隐式转换 Scala提供的隐式转换和隐式参数功能,是非常有特色的功能.是Java等编程语言所没有的功能.它可以允许你手动指定,将某种类型的对象转换成其他类型的对象或者是给一个类增加方法.通过这些功能, ...
- 2、Node.js 第一个应用
内容:三种变量申明方式,Node.js应用组成,第一个应用创建+代码 ################################################################# ...
- python process,queue
#-*- coding:utf-8 -*- from multiprocessing import Process,Queue import os,time,random def write(q): ...
- Protocols, Generics, and Existential Containers — Wait What?
For the longest time now, I thought that the two functions above were the same. But in actuality, wh ...
- BZOJ3224:普通平衡树(Splay)
Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 1. 插入x数 2. 删除x数(若有多个相同的数,因只删除一个) 3. 查询x数的排名(若有多个相 ...
- linq的左连接右连接内连接用法
1.左连接: var LeftJoin = from e in ListOfEmployees join d in ListOfDepartment on e.DeptID equals d.ID i ...
- 关于python接口基础到进阶随笔
想了很久,闲来无事,今天想了下还是总结了下写下来,部分参考官方源码理解,还有就是这么久的理解, 如果觉得有帮助请记得点赞 先讲下接口url组成拿后台服务为例 通常一个后台请求url格式: http:/ ...