Difference between revisions of "cpp/string/wide/wcslen"
From cppreference.com
m (Text replace - "{{example cpp" to "{{example") |
(Revert the falsification on 27 February 2015: the standard does not guarantee wchar_t can represent Unicode characters.) |
||
(9 intermediate revisions by 5 users not shown) | |||
Line 1: | Line 1: | ||
− | {{cpp/title| wcslen}} | + | {{cpp/title|wcslen}} |
− | {{cpp/string/wide/ | + | {{cpp/string/wide/navbar}} |
− | {{ddcl | header=cwchar | | + | {{ddcl|header=cwchar| |
− | std::size_t wcslen( const wchar_t *str ); | + | std::size_t wcslen( const wchar_t* str ); |
}} | }} | ||
− | Returns the length of a wide string, that is the number of non-null wide characters that precede the terminating null wide character. | + | Returns the length of a wide string, that is the number of non-null wide characters that precede the terminating null wide character. |
+ | |||
+ | The behavior is undefined if there is no null character in the wide character array pointed to by {{c|str}}. | ||
+ | |||
===Parameters=== | ===Parameters=== | ||
− | {{ | + | {{par begin}} |
− | {{ | + | {{par|str|pointer to the null-terminated wide string to be examined}} |
− | {{ | + | {{par end}} |
===Return value=== | ===Return value=== | ||
− | The length of the null-terminated wide string {{ | + | The length of the null-terminated wide string {{c|str}}. |
+ | |||
+ | ===Possible implementation=== | ||
+ | {{eq fun | ||
+ | |1= | ||
+ | std::size_t wcslen(const wchar_t* start) | ||
+ | { | ||
+ | // NB: start is not checked for nullptr! | ||
+ | const wchar_t* end = start; | ||
+ | while (*end != L'\0') | ||
+ | ++end; | ||
+ | return end - start; | ||
+ | } | ||
+ | }} | ||
===Example=== | ===Example=== | ||
{{example | {{example | ||
− | + | |code= | |
− | + | ||
#include <iostream> | #include <iostream> | ||
#include <cwchar> | #include <cwchar> | ||
Line 25: | Line 40: | ||
std::wcout << "The length of L\"" << str << "\" is " << std::wcslen(str) << '\n'; | std::wcout << "The length of L\"" << str << "\" is " << std::wcslen(str) << '\n'; | ||
} | } | ||
− | + | |output= | |
The length of L"Hello, world!" is 13 | The length of L"Hello, world!" is 13 | ||
}} | }} | ||
+ | |||
+ | ===See also=== | ||
+ | {{dsc begin}} | ||
+ | {{dsc inc|cpp/string/byte/dsc strlen}} | ||
+ | {{dsc inc|cpp/string/multibyte/dsc mblen}} | ||
+ | {{dsc see c|c/string/wide/wcslen}} | ||
+ | {{dsc end}} | ||
+ | |||
+ | {{langlinks|de|es|fr|it|ja|pt|ru|zh}} |
Latest revision as of 08:54, 18 August 2024
Defined in header <cwchar>
|
||
std::size_t wcslen( const wchar_t* str ); |
||
Returns the length of a wide string, that is the number of non-null wide characters that precede the terminating null wide character.
The behavior is undefined if there is no null character in the wide character array pointed to by str.
Contents |
[edit] Parameters
str | - | pointer to the null-terminated wide string to be examined |
[edit] Return value
The length of the null-terminated wide string str.
[edit] Possible implementation
std::size_t wcslen(const wchar_t* start) { // NB: start is not checked for nullptr! const wchar_t* end = start; while (*end != L'\0') ++end; return end - start; } |
[edit] Example
Run this code
#include <iostream> #include <cwchar> int main() { const wchar_t* str = L"Hello, world!"; std::wcout << "The length of L\"" << str << "\" is " << std::wcslen(str) << '\n'; }
Output:
The length of L"Hello, world!" is 13
[edit] See also
returns the length of a given string (function) | |
returns the number of bytes in the next multibyte character (function) | |
C documentation for wcslen
|