C ++ UTF-8实际字符串长度 [英] C++ UTF-8 actual string length

查看:656
本文介绍了C ++ UTF-8实际字符串长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在任何标准库中都有任何原生(跨平台) C ++ 函数返回 std :: string

更新:
我们知道 std :: string.length 返回字节数而不是字符数。
我已经有一个自定义函数返回实际的函数,但我正在寻找一个标准的函数。

解决方案

codecvt 应该是有用的,标准提供了UTF-8的实现,例如 codecvt_utf8< char32_t>()



可能是这样:

  wstring_convert< codecvt_utf8< char32_t>,char32_t>()。from_bytes(the_std_string).size()


Is there any native (cross platform) C++ function in any of standard libraries which returns the actual length of std::string?

Update: as we know std::string.length() returns the number of bytes not the number of characters. I already have a custom function which returns the actual one, but I'm looking for an standard one.

解决方案

codecvt ought to be helpful, the Standard provides implementations for UTF-8, for example codecvt_utf8<char32_t>() would be appropriate in this case.

Probably something like:

wstring_convert< codecvt_utf8<char32_t>, char32_t >().from_bytes(the_std_string).size()

这篇关于C ++ UTF-8实际字符串长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆