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