get length of `wchar_t*` in c++
83,666
Solution 1
If you want to know the size of a wchar_t
string (wchar_t *
), you want to use wcslen(3)
:
size_t wcslen (const wchar_t *ws);
Solution 2
Assuming that you want to get the length of null terminated C style string, you have two options:
-
#include <cwchar>
and usestd::wcslen (dimObjPrefix);
, - or
#include <string>
and usestd::char_traits<wchar_t>::length (dimObjPrefix);
.
Author by
Jacob
Software engineer with experience in desktop applications, database applications and web applications.
Updated on April 02, 2020Comments
-
Jacob about 4 years
Please, how can I find out the length of a variable of type
wchar_t*
in c++?code example below:
wchar_t* dimObjPrefix = L"retro_";
I would like to find out how many characters
dimObjPrefix
contains -
Joseph Quinsey about 14 yearsPerhaps sizeof (wchar_t), or wchar_t x followed by sizeof x?
-
Bertrand Marron about 14 years@Joseph, it took me a long time to realize what you meant, I edited. Thank you. Sorry for the confusion.
-
Joseph Quinsey about 14 yearsRegarding sizeof(), neither K&R nor the ANSI Rationale give any reason for this idiosyncrasy about parentheses; does any SO reader happen to know?
-
MSalters about 14 years
sizeof(type)
versussizeof expression
. Now ife
is an expression, then(e)
is also an expression, sosizeof (expression)
is implicitly also allowed. -
Mayur over 3 yearsIs it safe to use
wcslen
?