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:

  1. #include <cwchar> and use std::wcslen (dimObjPrefix);,
  2. or #include <string> and use std::char_traits<wchar_t>::length (dimObjPrefix);.
Share:
83,666
Jacob
Author by

Jacob

Software engineer with experience in desktop applications, database applications and web applications.

Updated on April 02, 2020

Comments

  • Jacob
    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
    Joseph Quinsey about 14 years
    Perhaps sizeof (wchar_t), or wchar_t x followed by sizeof x?
  • Bertrand Marron
    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
    Joseph Quinsey about 14 years
    Regarding sizeof(), neither K&R nor the ANSI Rationale give any reason for this idiosyncrasy about parentheses; does any SO reader happen to know?
  • MSalters
    MSalters about 14 years
    sizeof(type) versus sizeof expression. Now if e is an expression, then (e) is also an expression, so sizeof (expression) is implicitly also allowed.
  • Mayur
    Mayur over 3 years
    Is it safe to use wcslen?