Przejdź do głównej zawartości

std::string_view rfind() method

// (1) Const version only
size_type rfind( const basic_string& str, size_type pos = 0 ) const noexcept;

// (2) Const version only
size_type rfind( const CharT* s, size_type pos, size_type count ) const;

// (3) Const version only
size_type rfind( const CharT* s, size_type pos = 0 ) const;

// (4) Const version only
size_type rfind( CharT ch, size_type pos = 0 ) const noexcept;

// (5) Const version only
template < class StringViewLike >
size_type rfind( const StringViewLike& t, size_type pos = 0 ) const noexcept(/* see below */);

Finds the last substring equal to the given character sequence.

Search begins at pos, i.e. the found substring must not begin in a position following pos.
If npos or any value not smaller than size() - 1 is passed as pos, whole string will be searched.

  • (1) Finds the last occurrence of v in this view, starting at position pos.
  • (2) Equivalent to rfind(basic_string_view(std::addressof(c), 1), pos).
  • (3) Equivalent to rfind(basic_string_view(s, count), pos).
  • (4) Equivalent to rfind(basic_string_view(s), pos).

Parameters

  • v - view to search for
  • pos - position at which to start the search
  • count - length of substring to search for
  • s - pointer to a character string to search for
  • ch - character to search for

Return value

Position of the first character of the found substring, or npos if no such substring is found.

Complexity

O(size() * v.size()) at worst.

important

This section requires improvement. You can help by editing this doc page.

Exceptions

(none)

Example

#include <string_view>

int main()
{
using namespace std::literals;
constexpr auto N = std::string_view::npos;

static_assert(true
&& (6 == "AB AB AB"sv.rfind("AB"))
&& (6 == "AB AB AB"sv.rfind("ABCD", N, 2))
&& (3 == "AB AB AB"sv.rfind("AB", 5))
&& (2 == "B AB AB "sv.rfind("AB", 2))
&& (N == "B AB AB "sv.rfind("AB", 1))
&& (5 == "B AB AB "sv.rfind('A'))
&& (4 == "AB AB AB"sv.rfind('B', 4))
&& (N == "AB AB AB"sv.rfind('C'))
);
}
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.

std::string_view rfind() method

// (1) Const version only
size_type rfind( const basic_string& str, size_type pos = 0 ) const noexcept;

// (2) Const version only
size_type rfind( const CharT* s, size_type pos, size_type count ) const;

// (3) Const version only
size_type rfind( const CharT* s, size_type pos = 0 ) const;

// (4) Const version only
size_type rfind( CharT ch, size_type pos = 0 ) const noexcept;

// (5) Const version only
template < class StringViewLike >
size_type rfind( const StringViewLike& t, size_type pos = 0 ) const noexcept(/* see below */);

Finds the last substring equal to the given character sequence.

Search begins at pos, i.e. the found substring must not begin in a position following pos.
If npos or any value not smaller than size() - 1 is passed as pos, whole string will be searched.

  • (1) Finds the last occurrence of v in this view, starting at position pos.
  • (2) Equivalent to rfind(basic_string_view(std::addressof(c), 1), pos).
  • (3) Equivalent to rfind(basic_string_view(s, count), pos).
  • (4) Equivalent to rfind(basic_string_view(s), pos).

Parameters

  • v - view to search for
  • pos - position at which to start the search
  • count - length of substring to search for
  • s - pointer to a character string to search for
  • ch - character to search for

Return value

Position of the first character of the found substring, or npos if no such substring is found.

Complexity

O(size() * v.size()) at worst.

important

This section requires improvement. You can help by editing this doc page.

Exceptions

(none)

Example

#include <string_view>

int main()
{
using namespace std::literals;
constexpr auto N = std::string_view::npos;

static_assert(true
&& (6 == "AB AB AB"sv.rfind("AB"))
&& (6 == "AB AB AB"sv.rfind("ABCD", N, 2))
&& (3 == "AB AB AB"sv.rfind("AB", 5))
&& (2 == "B AB AB "sv.rfind("AB", 2))
&& (N == "B AB AB "sv.rfind("AB", 1))
&& (5 == "B AB AB "sv.rfind('A'))
&& (4 == "AB AB AB"sv.rfind('B', 4))
&& (N == "AB AB AB"sv.rfind('C'))
);
}
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.