std::wcsstr(3)
π std::wcsstr(3) π
okay bestie, so this one's from the C++ Standard Libary manual β yes that's literally how they spell it in the header, not me, don't come for me π β and it's like... the wide-string version of "where's that substring at". Let's get into it π
β¨ NAME β¨
std::wcsstr β literally just std::wcsstr, the name is the name, that's the whole NAME section, iconic of them honestly π«Ά
β¨ Synopsis β¨
It lives in the header <cwchar> π¦
const wchar_t* wcsstr( const wchar_t* dest, const wchar_t* src );
wchar_t* wcsstr( wchar_t* dest, const wchar_t* src );
So what does it do?? It finds the first occurrence of the wide string src in the wide string that dest points to. That's the whole vibe check π
β οΈ Heads up bestie: the terminating null characters are not compared. Not even a little. So don't be out here expecting null-terminator behaviour, because it's giving "I don't see them" π
β¨ Parameters β¨
destβ pointer to the null-terminated wide string you wanna examine π (the thing you're searching in, the main character of this operation)srcβ pointer to the null-terminated wide string you're searching for πΈ (the little icon you're hunting through the crowd)
β¨ Return value β¨
- Pointer to the first character of the found substring inside
destπ― - Or a null pointer if no such substring is found β no match, no pointer, that's the deal π
- Special case alert: if
srcpoints to an empty string, thendestis returned. Like, immediately. Empty search = you get the whole thing back, we don't even try π
β¨ Example β¨
There's a "Run this code" link on the original page btw β this is the code that's sitting behind it π
#include <clocale>
#include <cwchar>
#include <iostream>
int main()
{
wchar_t const* origin = L"γ’γ«γγ‘, γγΌγΏ, γ¬γ³γ, γ’γ«γγ‘, γγΌγΏ, γ¬γ³γ.";
wchar_t const* target = L"γγΌγΏ";
wchar_t const* result = origin;
std::setlocale(LC_ALL, "en_US.utf8");
std::wcout << L"Substring to find: \"" << target << L"\"\n"
<< L"String to search: \"" << origin << L"\"\n\n";
for (; (result = std::wcsstr(result, target)) != nullptr; ++result)
std::wcout << L"Found: \"" << result << L"\"\n";
}
look at that loop bestie β it keeps calling std::wcsstr on the result and bumping ++result so it doesn't just find the same one forever. that's the trick, that's the whole trick π§
β¨ Possible output β¨
Substring to find: "γγΌγΏ"
String to search: "γ’γ«γγ‘, γγΌγΏ, γ¬γ³γ, γ’γ«γγ‘, γγΌγΏ, γ¬γ³γ."
Found: "γγΌγΏ, γ¬γ³γ, γ’γ«γγ‘, γγΌγΏ, γ¬γ³γ."
Found: "γγΌγΏ, γ¬γ³γ."
two matches, found in order, slay β¨ note how each found line prints from the match all the way to the end of the string, because that's what a pointer to the first char of the substring is π
β¨ See also β¨
findβ finds the first occurrence of the given substring (public member function ofstd::basic_string<CharT,Traits,Allocator>) π the C++ string version, the comfy onestrstrβ finds the first occurrence of a substring of characters (function) π₯ the narrow-char sibling, same energy, smaller charswcschrβ finds the first occurrence of a wide character in a wide string (function) π¦ singular, one char onlywcsrchrβ finds the last occurrence of a wide character in a wide string (function) β note therβ it's backwards, it's the last one- C documentation for
wcsstrβ the C version is out there too, go look if you want π
okay that's the whole page, no cap π«Ά and for the record this came from http://cppreference.com and it's dated 2024.06.10, so if anything's changed since then, blame the calendar not me ππ go find your substrings bestie, periodt π β¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms