关于字符集的一些总结

关于字符集的一些总结:

1. std::wstring s(L"abc"); //L标识使用wchar_t,一个字符占用两个字节。

"A" = 41

"ABC" = 41 42 43

L"A" = 00 41

L"ABC" = 00 41 00 42 00 43

string, wstring相互之间的转换:

#include <locale>
#include <codecvt>
#include <string>
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
std::string narrow = converter.to_bytes(wide_utf16_source_string);
std::wstring wide = converter.from_bytes(narrow_utf8_source_string);
#include <string>
#include <codecvt>
#include <locale>

std::string input_str = "this is a -string-, which is a sequence based on the -char- type.";
std::wstring input_wstr = L"this is a -wide- string, which is based on the -wchar_t- type.";

// conversion
std::wstring str_turned_to_wstr = std::wstring_convert<std::codecvt_utf8<wchar_t>>().from_bytes(input_str);

std::string wstr_turned_to_str = std::wstring_convert<std::codecvt_utf8<wchar_t>>().to_bytes(input_wstr);

 

这两段代码写得非常清晰,均来源于stackoverflow。

去除中文标点符号和空格:


    //定义转换对象
    wstring_convert<codecvt_utf8<wchar_t>> conv;
    //按行读取文件
    while (!infile.eof()){
        string s;
        getline(infile, s);
        //转换成宽字节类型
        wstring ws = conv.from_bytes(s);
        wstring nws;
        //过滤每一行中的标点和空格
        for (wchar_t ch : ws){
            //检查是否是标点和空格
            if (!iswpunct(ch) && !iswspace(ch)){
                nws.push_back(ch);
            }
        }
        //将过滤后的文本重新转换成UTF-8编码的多字节类型
        string ns = conv.to_bytes(nws);
        //重新写回文件
        outfile << ns;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值