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的字符串,实现了字符串截取,索引,长度等功能~

#ifndef __IUTF8_STRING__
#define __IUTF8_STRING__

class iutf8string
{
    public:
        iutf8string(const std::string& );

        iutf8string(const char* );

        ~iutf8string();

    public:

        int length();

        std::string substring(int start_index, int length);

        std::string get(int index);

        iutf8string operator + (iutf8string& );

        std::string operator [](int index);

        std::string stlstring();

        const char* c_str();

        iutf8string utf8substr(int u8start_index, int u8_length);

        std::string substr(int u8start_index, int u8_length);

    private:

        std::string data;
        int* offerset;
        int _length;

        void refresh();
};

#endif
#include <iostream>
#include <string>
#include "iutf8string.h"

using namespace std;

iutf8string::iutf8string(const string& str)
{
    data = str;
    refresh();
}

iutf8string::iutf8string(const char* str)
{
    data = string(str);
    refresh();
}

iutf8string::~iutf8string()
{
    delete[] offerset;
}

string iutf8string::stlstring()
{
    return data;
}

const char* iutf8string::c_str()
{
    return data.c_str();
}

iutf8string iutf8string::operator +(iutf8string& ustr)
{
    string temp = data + ustr.stlstring();

    return iutf8string(temp);
}

int iutf8string::length()
{

    return _length;
}

string iutf8string::get(int index)
{
    if(index >= _length) return "";
    ] - offerset[index]);

    return temp;
}

string iutf8string::operator [](int index)
{
    if(index >= _length) return "";
    ] - offerset[index]);

    return temp;
}

string iutf8string::substr(int u8_start_index, int u8_length)
{
    if(u8_start_index + u8_length >= _length) return "";

    return data.substr(offerset[u8_start_index], offerset[u8_start_index+u8_length] - offerset[u8_start_index]);
}

iutf8string iutf8string::utf8substr(int u8_start_index, int u8_length)
{
    if(u8_start_index + u8_length >= _length) return iutf8string("");
    string ret = data.substr(offerset[u8_start_index], offerset[u8_start_index+u8_length] - offerset[u8_start_index]);

    return iutf8string(ret);
}

void iutf8string::refresh()
{
    int *tmp = new int[data.length()];
    ;
    ; i < data.length(); i++)
    {
        )||(!((()))
        {
            tmp[tmpidx] = i;
            tmpidx++;
        }
    }

    tmp[tmpidx] = data.length();

    int *tmp2 = new int[tmpidx];
    ; i < tmpidx; i++)
    {
        tmp2[i] = tmp[i];
    }

    delete[] tmp;
    offerset = tmp2;
    _length = tmpidx;
}

//----------------test code ----------------------------
int main()
{
    iutf8string str1("_我Love你!中国  ,!");
    cout << "字符串长度:" <<str1.length() <<endl;
    int i; cout << "[" ;
    ; i < str1.length(); i++)
    {
        cout << str1[i] << " ";
    }
    cout << "]" << endl;
    ,);
    cout << one << endl;

    string s1("我们都是好孩子!");
    iutf8string str2(s1);
    cout << "[" ;
    ; i < str2.length(); i++)
    {
        cout << str2[i] << " ";
    }
    cout << "]" << endl;
}

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

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. jdk 1.6.0_39 下载

    Java SE Development Kit 6u39 Product / File Description File Size Download password Linux x86 65.42 ...

  2. Lyft Level 5 Challenge 2018 - Final Round (Open Div. 2) B 1075B (思维)

    B. Taxi drivers and Lyft time limit per test 1 second memory limit per test 256 megabytes input stan ...

  3. C. The Fair Nut and String 递推分段形dp

    C. The Fair Nut and String 递推分段形dp 题意 给出一个字符串选择一个序列\({p_1,p_2...p_k}\)使得 对于任意一个\(p_i\) , \(s[p_i]==a ...

  4. Jenkins利用官网上的rpm源安装

    官网网址:https://pkg.jenkins.io/redhat/                (官网上有安装的命令,参考网址) 安装jdk yum install -y java-1.8.0- ...

  5. Jenkins的Pipeline脚本在美团餐饮SaaS中的实践(转)

    一.背景 在日常开发中,我们经常会有发布需求,而且还会遇到各种环境,比如:线上环境(Online),模拟环境(Staging),开发环境(Dev)等.最简单的就是手动构建.上传服务器,但这种方式太过于 ...

  6. cs231n学习笔记(二)图像分类

    图像分类可说是计算机视觉中的基础任务同时也是核心任务,做好分类可为检测,分割等高阶任务打好基础. 本节课主要讲了两个内容,K近邻和线性分类器,都是以猫的分类为例. 一. K近邻 以猫的分类为例,一张含 ...

  7. HttpServletResponse 解决中文乱码

    response.setHeader("Content-type", "text/html;charset=UTF-8"); response.setChara ...

  8. C#中web项目使用log4net日志

    我准备把log4net的实现方法放在一个类库项目中,别的项目直接引用该类库,即可进行日志的记录,操作步骤如下: 1.下载log4net.dll文件 2.在解决方案下创建类库项目 3.把log4net. ...

  9. Luogu P4095 [HEOI2013]Eden 的新背包问题 思维/动规

    当时一直在想前缀和...多亏张队提醒... 从1到n背次包,保存每一个状态下的价值,就是不要把第一维压掉:再从n到1背一次,同样记住每种状态: 然后询问时相当于是max(前缀+后缀),当然前缀后缀中间 ...

  10. android 快速开发框架

    https://github.com/lipanquan/LPQRapidDevelopmentFramework 依赖LPQLibrary https://github.com/lipanquan/ ...