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 24
13      cout << "Original string: " << string1
14         << " (find) "is" was found at: " << string1.find( "is"
15         << " (rfind) "is" was found at: " << string1.rfind( "is" );
16
17      // find 'o' at location 1
18      location = string1.find_first_of( "misop" );
19      cout << " (find_first_of) found '" << string1[ location ]
20         << "' from the group "misop" at: " << location;
21
22      // find 'o' at location 28
23      location = string1.find_last_of( "misop" );
24      cout << " (find_last_of) found '" << string1[ location ]
25         << "' from the group "misop" at: " << location;
26
27      // find '1' at location 8
28      location = string1.find_first_not_of( "noi spm" );
29      cout << " (find_first_not_of) '" << string1[ location ]
30         << "' is not contained in "noi spm" and was found at: "
31         << location;
32
33      // find '.' at location 13
34      location = string1.find_first_not_of( "12noi spm" );
35      cout << " (find_first_not_of) '" << string1[ location ]
36         << "' is not contained in "12noi spm" and was "
37         << "found at: " << location << endl;
38
39      // search for characters not in string1
40      location = string1.find_first_not_of(  
41         "noon is 12 pm; midnight is not." );
42      cout << " find_first_not_of("noon is 12 pm; midnight is not.")"
43         << " returned: " << location << endl;
44   } // end main


Original string:
noon is 12 pm; midnight is not.

(find) "is" was found at: 5
(rfind) "is" was found at: 24

(find_first_of) found 'o' from the group "misop" at: 1

(find_last_of) found 'o' from the group "misop" at: 28

(find_first_not_of) '1' is not contained in "noi spm" and was found at: 8

(find_first_not_of) '.' is not contained in "12noi spm" and was found at: 13

find_first_not_of("noon is 12 pm; midnight is not.") returned: -1


Fig. 19.6. Demonstrating the string find member functions.

String string1 is declared and initialized in line 9. Line 14 attempts to find "is" in string1 using function find. If "is" is found, the subscript of the starting location of that string is returned. If the string is not found, the value string::npos (a public static constant defined in class string) is returned. This value is returned by the string find-related functions to indicate that a substring or character was not found in the string.

Line 15 uses member function rfind to search string1 backward (i.e., right-to-left). If "is" is found, the subscript location is returned. If the string is not found, string::npos is returned. [Note: The rest of the find functions presented in this section return the same type unless otherwise noted.]

Line 18 uses member function find_first_of to locate the first occurrence in string1 of any character in "misop". The searching is done from the beginning of string1. The character 'o' is found in element 1.

Line 23 uses member function find_last_of to find the last occurrence in string1 of any character in "misop". The searching is done from the end of string1. The character 'o' is found in element 28.

Line 28 uses member function find_first_not_of to find the first character in string1 not contained in "noi spm". The character '1' is found in element 8. Searching is done from the beginning of string1.

Line 34 uses member function find_first_not_of to find the first character not contained in "12noi spm". The character '.' is found in element 13. Searching is done from the beginning of string1.

Lines 40–41 use member function find_first_not_of to find the first character not contained in "noon is 12 pm; midnight is not.". In this case, the string being searched contains every character specified in the string argument. Because a character was not found, string::npos (which has the value –1 in this case) is returned.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.221.113