std::strrchr(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– NAME πŸ’–

okay bestie, welcome to the std::strrchr(3) page in the C++ Standard Libary manual β€” yes, that's the header spelling, iykyk. The NAME is literally std::strrchr - std::strrchr, and this whole page is about finding the last match like it's the main character. πŸ’…


🌸 Synopsis 🌸

Defined in header <cstring> β€” that's where it lives, bestie. ✨

const char* strrchr( const char* str, int ch );
    char* strrchr(       char* str, int ch );

So what does it do? It finds the last occurrence of ch (after conversion to char) in the byte string pointed to by str. The terminating null character is considered to be a part of the string and can be found if searching for '\0'. That's the whole vibe check. πŸ”₯


πŸ’– Parameters πŸ’–

  • str β€” pointer to the null-terminated byte string to be analyzed
  • ch β€” character to search for

πŸŽ€ Return value πŸŽ€

Pointer to the found character in str, or null pointer if no such character is found. That's the whole return tea. 🫢


πŸ¦‹ Example πŸ¦‹

Here's the example, no cap β€” run this code:

// Run this code

#include <cstring>
#include <iostream>

int main()
{
    char input[] = "/home/user/hello.c";
    char* output = std::strrchr(input, '/');
    if (output)
        std::cout << output + 1 << '\n';
}

πŸ“ Output πŸ“

hello.c

🌈 See also 🌈

  • strchr β€” finds the first occurrence of a character (function)
  • wcsrchr β€” finds the last occurrence of a wide character in a wide string (function)
  • rfind β€” find the last occurrence of a substring (public member function of std::basic_string<CharT,Traits,Allocator>)
  • C documentation for strrchr

source/version: http://cppreference.com Β· date: 2024.06.10 Β· page: std::strrchr(3) β€” bye bestie, go find that last slash πŸ’–βœ¨πŸ’…

πŸ“¦ where this page comes from