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()
<source lang="cpp">
- 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";
}
</source>
equivalent of strspn()
<source lang="cpp">
- 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";
}
</source>
Find the first of any of these chars
<source lang="cpp">
- 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
*/ </source>
Find the first of any of these chars starting from the end
<source lang="cpp">
- 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
*/ </source>
Find the first that"s not in this set
<source lang="cpp">
- 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
*/ </source>
Find the first that"s not in this set, starting from the end
<source lang="cpp">
- 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
*/ </source>
search a sub string
<source lang="cpp">
- 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.
*/ </source>
Search from the beginning
<source lang="cpp">
- include <string>
- include <iostream>
int main( ) {
std::string s = "Search from the beginning"; std::cout << s.find("ar") << "\n"; // Search from the beginning
} /* 2
*/ </source>
Search from the end
<source lang="cpp">
- include <string>
- include <iostream>
int main( ) {
std::string s = "Search from the beginning"; std::cout << s.rfind("ar") << "\n"; // Search from the end
} /* 2
*/ </source>
string.find_first_not_of( substring )
<source lang="cpp">
- 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
*/ </source>
string.find_first_of( substring )
<source lang="cpp">
- 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
*/ </source>
string.find_last_of(substring)
<source lang="cpp">
- 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
*/ </source>
string.find(substring)
<source lang="cpp">
- 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
*/ </source>
string.rfind(substring)
<source lang="cpp">
- 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
*/ </source>
Use the STL find() algorithm to obtain an iterator to the start of the first "a"
<source lang="cpp">
- 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;
}
</source>
Using find with reverse iteration
<source lang="cpp">
- 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 .
*/ </source>