C++/String/string find
Содержание
- 1 equivalent of strcspn() and strpbrk()
- 2 equivalent of strspn()
- 3 Find the first of any of these chars
- 4 Find the first of any of these chars starting from the end
- 5 Find the first that"s not in this set
- 6 Find the first that"s not in this set, starting from the end
- 7 search a sub string
- 8 Search from the beginning
- 9 Search from the end
- 10 string.find_first_not_of( substring )
- 11 string.find_first_of( substring )
- 12 string.find_last_of(substring)
- 13 string.find(substring)
- 14 string.rfind(substring)
- 15 Use the STL find() algorithm to obtain an iterator to the start of the first "a"
- 16 Using find with reverse iteration
equivalent of strcspn() and strpbrk()
#include <iostream>
#include <string>
using namespace std;
int main( )
{
string typing( "The quick, brown fox jumps over the lazy dog" );
cout << "String: " << typing << endl;
// equivalent of strcspn() and strpbrk()
string::size_type index = typing.find_first_of( "aeiou" );
if( index != string::npos )
cout << "\nThe first lower-case vowel is at index " << index;
else
cout << "\nThere is no lower-case vowel in the string";
}
equivalent of strspn()
#include <iostream>
#include <string>
using namespace std;
int main( )
{
string typing( "The quick, brown fox jumps over the lazy dog" );
cout << "String: " << typing << endl;
// equivalent of strspn()
string::size_type index = typing.find_first_not_of( "aeiou" );
if( index != string::npos )
cout << "\nThe first letter that is not a lower-case vowel "
"is at index " << index;
else
cout << "\nAll letters in the string are lower-case vowels";
}
Find the first of any of these chars
#include <string>
#include <iostream>
int main( ) {
std::string s = "Search from the beginning";
std::cout << s.find_first_of("swi") // Find the first of any of these chars
<< "\n";
}
/*
19
*/
Find the first of any of these chars starting from the end
#include <string>
#include <iostream>
int main( ) {
std::string s = "Search from the beginning";
std::cout << s.find_last_of("abg") << "\n"; // Find the first of any of these chars starting from the end
}
/*
24
*/
Find the first that"s not in this set
#include <string>
#include <iostream>
int main( ) {
std::string s = "Search from the beginning";
std::cout << s.find_first_not_of("the") // Find the first that"s not in this set
<< "\n"; //
}
/*
0
*/
Find the first that"s not in this set, starting from the end
#include <string>
#include <iostream>
int main( ) {
std::string s = "Search from the beginning";
std::cout << s.find_last_not_of("from") // Find the first that"s not in this set, starting from the end
<< "\n";
}
/*
24
*/
search a sub string
#include <iostream>
#include <string>
using namespace std;
int main()
{
int i;
string s1 ="this is a test.";
string s2;
i = s1.find("is");
if(i!=string::npos) {
cout << "Match found at " << i << endl;
cout << "Remaining string is:\n";
s2.assign(s1, i, s1.size());
cout << s2;
}
cout << "\n\n";
return 0;
}
/*
Match found at 2
Remaining string is:
is is a test.
*/
Search from the beginning
#include <string>
#include <iostream>
int main( ) {
std::string s = "Search from the beginning";
std::cout << s.find("ar") << "\n"; // Search from the beginning
}
/*
2
*/
Search from the end
#include <string>
#include <iostream>
int main( ) {
std::string s = "Search from the beginning";
std::cout << s.rfind("ar") << "\n"; // Search from the end
}
/*
2
*/
string.find_first_not_of( substring )
#include <iostream>
using std::cout;
using std::endl;
#include <string>
using std::string;
int main()
{
string string1( "This is a test string!");
int location;
location = string1.find_first_not_of( "is" );
cout << "\n\n(find_first_not_of) "" << string1[ location ]
<< " " << location;
return 0;
}
/*
(find_first_not_of) "T 0
*/
string.find_first_of( substring )
#include <iostream>
using std::cout;
using std::endl;
#include <string>
using std::string;
int main()
{
string string1( "This is a test string!");
int location;
location = string1.find_first_of( "is" );
cout << "\n\n(find_first_of) found "" << string1[ location ]
<< "at: " << location;
return 0;
}
/*
(find_first_of) found "iat: 2
*/
string.find_last_of(substring)
#include <iostream>
using std::cout;
using std::endl;
#include <string>
using std::string;
int main()
{
string string1( "This is a test string!");
int location;
location = string1.find_last_of( "is" );
cout << "\n\n(find_last_of) found "" << string1[ location ]
<< " at: " << location;
return 0;
}
/*
(find_last_of) found "i at: 18
*/
string.find(substring)
#include <iostream>
using std::cout;
using std::endl;
#include <string>
using std::string;
int main()
{
string string1( "This is a test string!");
int location;
cout << "Original string:\n" << string1
<< "\n\n(find) \"is\" was found at: " << string1.find( "is" );
return 0;
}
/*
Original string:
This is a test string!
(find) "is" was found at: 2
*/
string.rfind(substring)
#include <iostream>
using std::cout;
using std::endl;
#include <string>
using std::string;
int main()
{
string string1( "This is a test string!");
int location;
cout << "Original string:\n" << string1
<< "\n\n(find) \"is\" was found at: " << string1.rfind( "is" );
return 0;
}
/*
Original string:
This is a test string!
(find) "is" was found at: 5
*/
Use the STL find() algorithm to obtain an iterator to the start of the first "a"
#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
string strA("This is a test.");
string::iterator itr = find(strA.begin(), strA.end(), "a");
return 0;
}
Using find with reverse iteration
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
string s("It is him.");
vector<char> vector1(s.begin(), s.end());
ostream_iterator<char> out(cout, " ");
cout << "chars from the last t to the beginning: ";
vector<char>::reverse_iterator r = find(vector1.rbegin(), vector1.rend(), "t");
copy(r, vector1.rend(), out); cout << endl;
cout << "chars from the last t to the end: ";
copy(r.base() - 1, vector1.end(), out); cout << endl;
return 0;
}
/*
chars from the last t to the beginning: t I
chars from the last t to the end: t i s h i m .
*/