最近项目中用到需要给出每一个字在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

最新文章

  1. 解读ASP.NET 5 &amp; MVC6系列(14):View Component
  2. PAP认证方式原理和实现
  3. springmvc整合mybatis框架源码 bootstrap
  4. HYSBZ 2957 分块
  5. 数据迁移工具sqoop
  6. KO Demo
  7. 为什么你不应该自行更新 Drupal 网站?
  8. 英文版firefox显示中文字体丑的问题
  9. poj2823Sliding Window(线段树求最值)
  10. Mac OS X 在Finder新建文本文件
  11. MySql Error 2006
  12. Swift - 使用NSURLSession同步获取数据(通过添加信号量)
  13. 机器学习笔记-1 Linear Regression(week 1)
  14. Jmeter - foreach控制器之嵌套使用
  15. msql分区
  16. day 23-1 类的命名空间、组合
  17. python+opencv读取视频,调用摄像头
  18. timestamp 与 nonce 防止重放攻击
  19. POJ 1904 King&amp;#39;s Quest(强连通)
  20. Docker容器使用jenkins部署web项目--总结(二)

热门文章

  1. 常用插件html
  2. JQuery 处理 微擎传递过去数据
  3. NJU 操作系统实验三
  4. 八、asynicio模块以及爬虫应用asynicio模块(高性能爬虫)
  5. python深度学习:矩阵转置(transpose)
  6. PHP基础教程探讨一些php编程性能优化总结
  7. grpc:What is gRPC
  8. [CF1093G]Multidimensional Queries 题解
  9. [BZOJ1697][USACO2007 FEB]Cow Sorting牛排序:贪心+置换
  10. PO,BO,VO和POJO的区别