string: find( ) and rfind( )
![string: find( ) and rfind( )](http://www.java2s.com/Code/CppImages/stringfindandrfind.PNG)
#include <iostream>
#include <string>
using namespace std;
int main()
{
int i;
string stringObject1 = "Quick of Mind, Strong of Body, Pure of Heart";
string stringObject2;
i = stringObject1.find("Quick");
if(i!=string::npos) {
cout << "Match found at " << i << endl;
cout << "Remaining string is:\n";
stringObject2.assign(stringObject1, i, stringObject1.size());
cout << stringObject2;
}
cout << "\n\n";
i = stringObject1.find("Strong");
if(i!=string::npos) {
cout << "Match found at " << i << endl;
cout << "Remaining string is:\n";
stringObject2.assign(stringObject1, i, stringObject1.size());
cout << stringObject2;
}
cout << "\n\n";
i = stringObject1.find("Pure");
if(i!=string::npos) {
cout << "Match found at " << i << endl;
cout << "Remaining string is:\n";
stringObject2.assign(stringObject1, i, stringObject1.size());
cout << stringObject2;
}
cout << "\n\n";
// find list "of"
i = stringObject1.rfind("of");
if(i!=string::npos) {
cout << "Match found at " << i << endl;
cout << "Remaining string is:\n";
stringObject2.assign(stringObject1, i, stringObject1.size());
cout << stringObject2;
}
return 0;
}
Related examples in the same category
1. | String type class | | ![String type class](http://www.java2s.com/Code/CppImages/Stringtypeclass.PNG) |
2. | A filter to remove white-space characters at the ends of lines. | | ![A filter to remove white-space characters at the ends of lines.](http://www.java2s.com/Code/CppImages/Afiltertoremovewhitespacecharactersattheendsoflines.PNG) |
3. | A string demonstration: assignment, concatenate, compare | | ![A string demonstration: assignment, concatenate, compare](http://www.java2s.com/Code/CppImages/Astringdemonstrationassignmentconcatenatecompare.PNG) |
4. | Demonstrate insert(), erase(), and replace(). | | ![Demonstrate insert(), erase(), and replace().](http://www.java2s.com/Code/CppImages/Demonstrateinserteraseandreplace.PNG) |
5. | Use string: find, string::npos | | ![Use string: find, string::npos](http://www.java2s.com/Code/CppImages/Usestringfindstringnpos.PNG) |
6. | Strings: size, iterator, count, begin and end | | ![Strings: size, iterator, count, begin and end](http://www.java2s.com/Code/CppImages/Stringssizeiteratorcountbeginandend.PNG) |
7. | Print a name in two different formats | | ![Print a name in two different formats](http://www.java2s.com/Code/CppImages/Printanameintwodifferentformats.PNG) |
8. | Several string operations: substr | | ![Several string operations: substr](http://www.java2s.com/Code/CppImages/Severalstringoperationssubstr.PNG) |
9. | String Char Indexing | | ![String Char Indexing](http://www.java2s.com/Code/CppImages/StringCharIndexing.PNG) |
10. | String Find and replace | | ![String Find and replace](http://www.java2s.com/Code/CppImages/StringFindandreplace.PNG) |
11. | String Size | | ![String Size](http://www.java2s.com/Code/CppImages/StringSize.PNG) |
12. | String SizeOf | | ![String SizeOf](http://www.java2s.com/Code/CppImages/StringSizeOf.PNG) |
13. | A short string demonstration | | ![A short string demonstration](http://www.java2s.com/Code/CppImages/Ashortstringdemonstration.PNG) |
14. | String insert(), erase(), and replace() | | ![String insert(), erase(), and replace()](http://www.java2s.com/Code/CppImages/Stringinserteraseandreplace.PNG) |
15. | string variable instead of a character array | | ![string variable instead of a character array](http://www.java2s.com/Code/CppImages/stringvariableinsteadofacharacterarray.PNG) |
16. | Inputting Multiple Words into a String | | ![Inputting Multiple Words into a String](http://www.java2s.com/Code/CppImages/InputtingMultipleWordsintoaString.PNG) |
17. | Adding Strings | | ![Adding Strings](http://www.java2s.com/Code/CppImages/AddingStrings.PNG) |
18. | Read string from console | | ![Read string from console](http://www.java2s.com/Code/CppImages/Readstringfromconsole.PNG) |
19. | Insert, search, and replace in strings. | | ![Insert, search, and replace in strings.](http://www.java2s.com/Code/CppImages/Insertsearchandreplaceinstrings.PNG) |
20. | Accessing Characters In Strings | | ![Accessing Characters In Strings](http://www.java2s.com/Code/CppImages/AccessingCharactersInStrings.PNG) |