c++的字符串中的每一个元素都是一个字节。所以在装入utf8字符串的时候,其实是按照一定的规则编码的。

字符的8位中 如果0开头 则自己就是一个单位。

1字节 0xxxxxxx 
2字节 110xxxxx 10xxxxxx 
3字节 1110xxxx 10xxxxxx 10xxxxxx
4字节 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
5字节 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
6字节 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx

所以知道这个就好办了。这里有一个类,用来专门处理utf-8的字符串,实现了字符串截取,索引,长度等功能~

  1. #ifndef __IUTF8_STRING__
  2. #define __IUTF8_STRING__
  3.  
  4. class iutf8string
  5. {
  6. public:
  7. iutf8string(const std::string& );
  8.  
  9. iutf8string(const char* );
  10.  
  11. ~iutf8string();
  12.  
  13. public:
  14.  
  15. int length();
  16.  
  17. std::string substring(int start_index, int length);
  18.  
  19. std::string get(int index);
  20.  
  21. iutf8string operator + (iutf8string& );
  22.  
  23. std::string operator [](int index);
  24.  
  25. std::string stlstring();
  26.  
  27. const char* c_str();
  28.  
  29. iutf8string utf8substr(int u8start_index, int u8_length);
  30.  
  31. std::string substr(int u8start_index, int u8_length);
  32.  
  33. private:
  34.  
  35. std::string data;
  36. int* offerset;
  37. int _length;
  38.  
  39. void refresh();
  40. };
  41.  
  42. #endif
  1. #include <iostream>
  2. #include <string>
  3. #include "iutf8string.h"
  4.  
  5. using namespace std;
  6.  
  7. iutf8string::iutf8string(const string& str)
  8. {
  9. data = str;
  10. refresh();
  11. }
  12.  
  13. iutf8string::iutf8string(const char* str)
  14. {
  15. data = string(str);
  16. refresh();
  17. }
  18.  
  19. iutf8string::~iutf8string()
  20. {
  21. delete[] offerset;
  22. }
  23.  
  24. string iutf8string::stlstring()
  25. {
  26. return data;
  27. }
  28.  
  29. const char* iutf8string::c_str()
  30. {
  31. return data.c_str();
  32. }
  33.  
  34. iutf8string iutf8string::operator +(iutf8string& ustr)
  35. {
  36. string temp = data + ustr.stlstring();
  37.  
  38. return iutf8string(temp);
  39. }
  40.  
  41. int iutf8string::length()
  42. {
  43.  
  44. return _length;
  45. }
  46.  
  47. string iutf8string::get(int index)
  48. {
  49. if(index >= _length) return "";
  50. ] - offerset[index]);
  51.  
  52. return temp;
  53. }
  54.  
  55. string iutf8string::operator [](int index)
  56. {
  57. if(index >= _length) return "";
  58. ] - offerset[index]);
  59.  
  60. return temp;
  61. }
  62.  
  63. string iutf8string::substr(int u8_start_index, int u8_length)
  64. {
  65. if(u8_start_index + u8_length >= _length) return "";
  66.  
  67. return data.substr(offerset[u8_start_index], offerset[u8_start_index+u8_length] - offerset[u8_start_index]);
  68. }
  69.  
  70. iutf8string iutf8string::utf8substr(int u8_start_index, int u8_length)
  71. {
  72. if(u8_start_index + u8_length >= _length) return iutf8string("");
  73. string ret = data.substr(offerset[u8_start_index], offerset[u8_start_index+u8_length] - offerset[u8_start_index]);
  74.  
  75. return iutf8string(ret);
  76. }
  77.  
  78. void iutf8string::refresh()
  79. {
  80. int *tmp = new int[data.length()];
  81. ;
  82. ; i < data.length(); i++)
  83. {
  84. )||(!((()))
  85. {
  86. tmp[tmpidx] = i;
  87. tmpidx++;
  88. }
  89. }
  90.  
  91. tmp[tmpidx] = data.length();
  92.  
  93. int *tmp2 = new int[tmpidx];
  94. ; i < tmpidx; i++)
  95. {
  96. tmp2[i] = tmp[i];
  97. }
  98.  
  99. delete[] tmp;
  100. offerset = tmp2;
  101. _length = tmpidx;
  102. }
  103.  
  104. //----------------test code ----------------------------
  105. int main()
  106. {
  107. iutf8string str1("_我Love你!中国 ,!");
  108. cout << "字符串长度:" <<str1.length() <<endl;
  109. int i; cout << "[" ;
  110. ; i < str1.length(); i++)
  111. {
  112. cout << str1[i] << " ";
  113. }
  114. cout << "]" << endl;
  115. ,);
  116. cout << one << endl;
  117.  
  118. string s1("我们都是好孩子!");
  119. iutf8string str2(s1);
  120. cout << "[" ;
  121. ; i < str2.length(); i++)
  122. {
  123. cout << str2[i] << " ";
  124. }
  125. cout << "]" << endl;
  126. }

最后祝您,提乾涉经。告辞。

c++ 处理utf-8字符串的更多相关文章

  1. 聊聊Node.js 独立日漏洞

    背景 Node.js 社区近期在美国独立日周末的狂欢之时爆出漏洞 https://medium.com/@iojs/important-security-upgrades-for-node-js-an ...

  2. Python基础2 编码和逻辑运算符

    编码: AscII码 :标准ASCII码是采用7位二进制码来编码的,当用1个字节(8位二进制码)来表示ASCII码时,就在最高位添加1个0. 一个英文字母占一个字节 8位(bit)==一个字节(byt ...

  3. python基础(三)编码,深浅copy

    编码,深浅copy encode,decode在python2中使用的一些迷糊,python3中更容易理解 要理解encode和decode,首先我们要明白编码,字符和二进制的相关概念,简单来说,计算 ...

  4. 廖雪峰Python3笔记

    主要复习过一遍 简介 略 安装 略 *** 第一个Python程序 第一行的注释: # _*_ coding: utf-8 _*_ #!/usr/bin/env python3 print() 可以接 ...

  5. Python基础2 字符编码和逻辑运算符

    编码 AscII码 :标准ASCII码是采用7位二进制码来编码的,最高为0,没有0000 0000,所以就是2**7-1=127个字符 , 当用1个字节(8位二进制码)来表示ASCII码时,就在最高位 ...

  6. Python小白学习之路(十六)—【内置函数一】

    将68个内置函数按照其功能分为了10类,分别是: 数学运算(7个) abs()   divmod()  max()  min()  pow()  round()  sum() 类型转换(24个) bo ...

  7. Java中I/O流之数据流

    Java 中的数据流: 对于某问题:将一个 long 类型的数据写到文件中,有办法吗?    转字符串 → 通过 getbytes() 写进去,费劲,而且在此过程中 long 类型的数需要不断地转换. ...

  8. java 聊天程序

    package cn.apr.chart; import java.net.*; import java.io.*; import java.util.*; public class ChatServ ...

  9. python3.7 文件操作

    #!/usr/bin/env python __author__ = "lrtao2010" #python3.7 文件操作 # r 只读,默认打开方式,当文件不存在时会报错 # ...

  10. JNI教程与技术手册

    转载请标明出处:http://blog.csdn.net/shensky711/article/details/52806794 本文出自: [HansChen的博客] 概述 对于JNI,有些童鞋在没 ...

随机推荐

  1. springmvc htmlEscape标签的作用

    有些东西自己不知道就想要弄明白 唉   做项目 看人家项目中用到啦 不会 不知道 就百度啦 整理了一下 方便自己记忆 一.SpringMVC 表单元素标签 如下: <form:textarea ...

  2. linode出现以下报错

    Linode Manager 报错 系统重新安装后 解决办法执行  rm -rf ~/.ssh/known_hosts 再继续执行:ssh root@72.14.189.163

  3. Gym - 101615J Grid Coloring DP 2017-2018 ACM-ICPC Pacific Northwest Regional Contest (Div. 1)

    题目传送门 题目大意: 给出n*m的网格,有红蓝两种颜色,每个格子都必须被染色,当一个格子被染成蓝色后,这个格子左上方的一块都必须被染成蓝色,问最后的方案数量. 思路: 按照题目条件,如果有一个格子被 ...

  4. lintcode - 恢复ip地址

    class Solution { public: /* * @param s: the IP string * @return: All possible valid IP addresses */ ...

  5. PS常用快捷键大全

    察看图像类别 说明:: --- Shift键 : --- 空格键         *--- 在Imageready中不适用 § --- 只在Imageready中可用 动作 结果 双击工具箱::或Ct ...

  6. linux学习3(vim)

    一.文档编辑 1. vi和vim命令 Vim的打开文件的方式(4种,要求掌握的就前三种): 1. vim 文件路径                                            ...

  7. BeautifulSoup4模块的使用

    1. 安装 pip3 install beautifulsoup42. 使用 from bs4 import BeautifulSoup obj = BeautifulSoup("HTML内 ...

  8. 1.Ioc&DI和Spring

    1.面向对象回顾和案例 面向对象程序设计:1 2 3 4 案例分析: 需求分析: 报表功能:     报表服务类,检索数据,并生成图标     报表生成器类,生成不同格式的报表文件,例如PDF格式.H ...

  9. bootstrap多选框

    不多说,先上图片 本多选框是用的bootstrap的样式为基础,将弹出框css改造,然后自己写的js得到. 下面为全部页面的代码,需要的可以自己改动js,得到自己需要的效果 <!DOCTYPE ...

  10. 懒汉式单例要加volatile吗

    private static volatile Something instance = null; public static Something getInstance() { if (insta ...