Finding Strings and Characters in a string

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

Figure 18.6. Demonstrating the string find functions.

(This item is displayed on pages 894 - 895 in the print version)

1 // Fig. 18.6: Fig18_06.cpp 2 // Demonstrating the string find member functions. 3 #include 4 using std::cout; 5 using std::endl; 6 7 #include 8 using std::string; 9 10 int main() 11 { 12 string string1( "noon is 12 pm; midnight is not." ); 13 int location; 14 15 // find "is" at location 5 and 25 16 cout << "Original string: " << string1 17 << " (find) "is" was found at: " << string1.find( "is" ) 18 << " (rfind) "is" was found at: " << string1.rfind( "is" ); 19 20 // find 'o' at location 1 21 location = string1.find_first_of( "misop" ); 22 cout << " (find_first_of) found '" << string1[ location ] 23 << "' from the group "misop" at: " << location; 24 25 // find 'o' at location 29 26 location = string1.find_last_of( "misop"); 27 cout << " (find_last_of) found '" << string1[ location ] 28 << "' from the group "misop" at: " << location; 29 30 // find '1' at location 8 31 location = string1.find_first_not_of( "noi spm" ); 32 cout << " (find_first_not_of) '" << string1[ location ] 33 << "' is not contained in "noi spm" and was found at:" 34 << location; 35 36 // find '.' at location 12 37 location = string1.find_first_not_of( "12noi spm" ); 38 cout << " (find_first_not_of) '" << string1[ location ] 39 << "' is not contained in "12noi spm" and was " 40 << "found at:" << location << endl; 41 42 // search for characters not in string1 43 location = string1.find_first_not_of( 44 "noon is 12 pm; midnight is not." ); 45 cout << " find_first_not_of("noon is 12 pm; midnight is not.")" 46 << " returned: " << location << endl; 47 return 0; 48 } // end main  

Original string: noon is 12 pm; midnight is not. (find) "is" was found at: 5 (rfind) "is" was found at: 25 (find_first_of) found 'o' from the group "misop" at: 1 (find_last_of) found 'o' from the group "misop" at: 29 (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:12 find_first_not_of("noon is 12 pm; midnight is not.") returned: -1  


String string1 is declared and initialized in line 12. Line 17 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 18 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 21 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 26 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 29.

Line 31 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 37 uses member function find_first_not_of to find the first character not contained in "12noi spm". The character '.' is found in element 12. Searching is done from the end of string1.

Lines 4344 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.

Категории