19.7. Finding Substrings and Characters in a string

Class string provides const member functions for finding substrings and characters in a string. Figure 19.6 demonstrates the find functions.

 1   // Fig. 19.6: Fig19_06.cpp 2   // Demonstrating the string find member functions. 3   #include <iostream> 4   #include <string> 5   using namespace std; 6  7   int main() 8   { 9      string string1( "noon is 12 pm; midnight is not." );10      int location;11 12      // find "is" at location 5 and 2413      cout << "Original string:\n" << string114         << "\n\n(find) \"is\" was found at: " << string1.find( "is"15         << "\n(rfind) \"is\" was found at: " << string1.rfind( "is" );16 17      // find 'o' at location ...

Get C++11 for Programmers, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.