c++读取utf-8格式中英文混合string
最近项目中用到需要给出每一个字在string中的索引,但是又因为中文字符跟英文字符长度不一样,得分开处理,
在这里记录一下。
想要达到的效果如下:
将 “测试3.1415engEng”分割开

代码:
std::vector <std::string> splitEachChar(const string chars)
{
std::vector<std::string> words;
std::string input(chars);
int len = input.length();
int i = ; while (i < len) {
assert ((input[i] & 0xF8) <= 0xF0);
int next = ;
if ((input[i] & 0x80) == 0x00) {
std::cout << "one character: " << input[i] << std::endl;
} else if ((input[i] & 0xE0) == 0xC0) {
next = ;
std::cout << "two character: " << input.substr(i, next) << std::endl;
} else if ((input[i] & 0xF0) == 0xE0) {
next = ;
std::cout << "three character: " << input.substr(i, next) << std::endl;
} else if ((input[i] & 0xF8) == 0xF0) {
next = ;
std::cout << "four character: " << input.substr(i, next) << std::endl;
}
words.push_back(input.substr(i, next));
i += next;
}
return words;
}
void testtemp()
{
string input;
while ()
{
getline(cin,input);
if(input == "exit") break;
cout<<"--------------------------------"<<endl;
vector <std::string> ret = splitEachChar(input); cout<<input<<endl;
for(auto it : ret)cout<<it<<endl;
cout<<"--------------------------------"<<endl;
}
}
int main()
{
testtemp();
return ;
}
参考:
https://blog.csdn.net/cy_tec/article/details/87884177
c++读取utf-8格式中英文混合string的更多相关文章
- 实现中英文混合string的逆向输出
#include <iostream> using namespace std; // 输入一个字符串(包括英文和中文),将其反序输出, 如: // hello 今天真热 ---> ...
- c#的中英文混合字符串截取 public static string SubString(string inputString, int byteLength)
/// <summary> /// c#的中英文混合字符串截取(区分中英文) /// </summary> /// <param ...
- C#与JS实现 获取指定字节长度 中英文混合字符串 的方法
平时在作数据库插入操作时,如果用 INSERT 语句向一个varchar型字段插入内容时,有时会因为插入的内容长度超出规定的长度而报错. 尤其是插入中英文混合字符串时,SQL Server中一般中文要 ...
- c#的中英文混合字符串截取
public class StringHelper { public static string GetSubString(string str, int len) ...
- ThinkPHP+Smarty模板中截取包含中英文混合的字符串乱码的解决方案
好几天没写博客了,其实有好多需要总结的,因为最近一直在忙着做项目,但是困惑了几天的Smarty模板中截取包含中英文混合的字符串乱码的问题,终于解决了,所以记录下来,需要的朋友看一下: 出现乱码的原因: ...
- DataTable to Excel(使用NPOI、EPPlus将数据表中的数据读取到excel格式内存中)
/// <summary> /// DataTable to Excel(将数据表中的数据读取到excel格式内存中) /// </summary> /// <param ...
- Lua截取utf-8编码的中英文混合字符串
参考博客:UTF8字符串在lua的截取和字数统计[转载] 需求 按字面个数来截取子字符串 函数(字符串, 开始位置, 截取长度) utf8sub(,) = 好1世界哈 utf8sub(,) = 你好1 ...
- PHP获取中英文混合字符串长度及截取
1.字符串长度 PHP获取中英文混合字符串长度的实现代码如下,1中文=1位,2英文=1位,可自行修改 /** * PHP获取字符串中英文混合长度 * @param $str string 字符串 * ...
- php截取等长UFT8中英文混合字串
由于需要,想实现“php截取等长UFT8中英文混合字串”,可是网上找了很多代码不是有乱码就是不能实现等长(以一个中文长度为单位,两个英文字母算一个长度,如‘等长’长度为2,‘UTF8’长度也是2). ...
随机推荐
- 解决deepin没有ll等命令的办法
编辑~/.bashrc, 添加alias 如下 vim ~/.bashrc 设置别名. 添加如下行 alias ll='ls -alF' alias la='ls -A' alias vi='vim' ...
- 【转】Linux内核中分配4M以上大内存的方法
在Linux内核中, kmalloc能够分配的最大连续内存为2的(MAX_ORDER-1)次方个page(参见alloc_pages函数, "if (unlikely(order & ...
- pytho xml
转载自:https://www.cnblogs.com/gouguoqilinux/p/9168332.html xml是实现不同语言或程序直接进行数据交换的协议,跟json差不多,单json使用起来 ...
- 界面开发工具集Telerik UI for ASP.NET AJAX发布R2 2019
Telerik UI for ASP.NET AJAX拥有构建ASP.NET AJAX和SharePoint应用程序的80+控件,将为任何浏览器和设备构建Web Forms应用程序的时间缩短一半.Te ...
- VM架构设计文档初稿v0.01
VM架构设计文档初稿v0.01 文档介绍 本文档是经过讨论,作为VM新架构设计开发中的重要依据.对该架构的整个系统的结构进行详实细致的描述.阐述框架结构,说明该架构所采取的设计策略和所有技术,并对相关 ...
- Ionic4 & Cordova 打包Android,含签名
IOS打包必须在Mac环境,以下讲述安卓打包并签名的过程,若是不签名,那么APP不能在手机上安装. 1.添加对应的android框架 ionic cordova prepare android 1)修 ...
- pyqt5--动画
动画类别继承结构图 天子骄龙
- buuctf@rip
exp: from pwn import * buf_start = 0x00007FFDBD754931 buf_end = 0x00007FFDBD754948 func_fun_addr= 0x ...
- python3中OpenCV imwrite保存中文路径文件
原先一段将特征值保存为图片的代码,这部分学生的电脑上运行没有生成图片 代码的基本样子是: import os import cv2 import numpy as np def text_to_pic ...
- 【NOIP2016提高A组模拟9.17】序列
题目 分析 首先用\(a_i\)表示达到目标的步数\(B_i-A_i(mod 4)\) 根据粉刷栅栏,先不管mod 4的情况,答案就是\(\sum\max(a_i-a_{i+1},0)\) 那我们刚才 ...