String Searching

The string class provides six search functions, each with four prototypes. Let's describe them briefly.

The find() Family

Here are the find() prototypes:

size_type find (const basic_string& str, size_type pos = 0) const;
size_type find (const charT* s, size_type pos = 0) const;
size_type find (const charT* s, size_type pos, size_type n) const;
size_type find (charT c, size_type pos = 0) const;

The first member returns the position of the beginning of the first occurrence of the substring str in the invoking object; with the search beginning at position pos. If the substring is not found, the method returns npos.

 string longer("That is a funny hat."); string shorter("hat"); size_type loc1 = longer.find(shorter); // sets loc1 ...

Get The Waite Group's C++ Primer Plus, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.