在 C++ 中获取 `wchar_t*` 的长度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2853615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:17:32  来源:igfitidea点击:

get length of `wchar_t*` in c++

c++stringwchar-t

提问by Jacob

Please, how can I find out the length of a variable of type wchar_t*in c++?

请问,我怎样才能找出wchar_t*C++中类型变量的长度?

code example below:

下面的代码示例:

wchar_t* dimObjPrefix = L"retro_";

I would like to find out how many characters dimObjPrefixcontains

我想知道dimObjPrefix包含多少个字符

回答by Bertrand Marron

If you want to know the size of a wchar_tstring(wchar_t *), you want to use wcslen(3):

如果您想知道wchar_t字符串( wchar_t *)的大小,您需要使用wcslen(3)

size_t wcslen (const wchar_t *ws);

回答by wilx

Assuming that you want to get the length of null terminated C style string, you have two options:

假设您要获取空终止 C 样式字符串的长度,您有两个选择:

  1. #include <cwchar>and use std::wcslen (dimObjPrefix);,
  2. or #include <string>and use std::char_traits<wchar_t>::length (dimObjPrefix);.
  1. #include <cwchar>并使用std::wcslen (dimObjPrefix);
  2. #include <string>并使用std::char_traits<wchar_t>::length (dimObjPrefix);.