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). ...
随机推荐
- poj 1915 KnightMoves(bfs)
Knight Moves Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 24094 Accepted: 11364 De ...
- Linux学习--第十一天--source、环境变量目录、欢迎信息、正则、cut、awk、sed、sort、判断表达式、if、for、case、一些脚本
source source /root/.bashrc #让修改后的配置文件在不重启系统的情况下生效.source等同于. 环境变量目录 /etc/profile /etc/profile.d/*.s ...
- 云主机使用ansible出现秘钥认证问题
使用ansible的时候,出现如下秘钥失效的问题: root@jumpserver ftp]# ansible web -m ping The authenticity of host 'web-00 ...
- oracle sql 高级编程 历史笔记整理
20130909 周一 oracle sql 开发指南 第7章 高级查询 1.层次化查询select level,ttt.*,sys_connect_by_path(ttt.col1,',') fro ...
- Java AtomicInteger类的使用方法详解_java - JAVA
文章来源:嗨学网 敏而好学论坛www.piaodoo.com 欢迎大家相互学习 首先看两段代码,一段是Integer的,一段是AtomicInteger的,为以下: public class Samp ...
- 【NOIP2013模拟】水叮当的舞步
题目 水叮当得到了一块五颜六色的格子形地毯作为生日礼物,更加特别的是,地毯上格子的颜色还能随着踩踏而改变. 为了讨好她的偶像虹猫,水叮当决定在地毯上跳一支轻盈的舞来卖萌~~~ 地毯上的格子有N行N列, ...
- React 项目 ant design 的 CheckboxGroup 验证
使用 ant design 提供的 getFieldDecorator 进行验证 一般开始使用默认选中 <FormItem> {getFieldDecorator('checkProtoc ...
- 【技术分享:python 应用之一】如何使用 Python 对 Excel 做一份数据透视表
客户这边,其中有一张如同上图所示的数据汇总表,然而需求是,需要将这张表数据做一个数据透视表,最后通过数据透视表中的数据,填写至系统数据库.拿到需求,首先就想到肯定不能直接用设计器去操作 Excel,通 ...
- Springboot对Controller层方法进行统一异常处理
Controller层方法,进行统一异常处理 提供两种不同的方案,如下: 方案1:使用 @@ControllerAdvice (或@RestControllerAdvice), @ExceptionH ...
- 大哥带我们的mysql注入 基于bool的盲注
盲注 那么我们来了解一点盲注的语法 这里面是语法的介绍 https://blog.csdn.net/alex_seo/article/details/82148955 0X01第一步我们先判断当前数据 ...