它在字符串中搜索字符串末尾的第一个字符,该字符与其参数中指定的任何字符都不匹配。 返回值: 成功时第一个不匹配字符的索引,如果未找到此类字符,则为string::npos。 语法1: 搜索不是字符串str元素的最后一个字符。
null
size_type string::find_last_not_of (const string& str) conststr : Another string with the set of charactersto be used in the search.
CPP
// CPP code for find_last_not_of (const string& str) const #include <iostream> using namespace std; // Function to demonstrate find_last_not_of void find_last_not_ofDemo(string str1, string str2) { // Finds last character in str1 which // is not present in str2 string::size_type ch = str1.find_last_not_of(str2); cout << "First unmatched character : " ; cout << str1[ch]; } // Driver code int main() { string str1( "Hello World!" ); string str2( "GeeksforGeeks" ); cout << "Original String : " << str1 << endl; cout << "String to be looked in : " << str2 << endl; find_last_not_ofDemo(str1, str2); return 0; } |
输出:
Original String : Hello World!String to be looked in : GeeksforGeeksFirst unmatched character : !
语法2: 从索引idx中搜索不是字符串str元素的最后一个字符。
size_type string::find_last_not_of (const string& str, size_type idx) conststr : Another string with the set of charactersto be used in the search.idx : is the index number from where we have tostart finding first unmatched character.
CPP
// CPP code for string::find_last_not_of // (const string& str, size_type idx) const #include <iostream> using namespace std; // Function to demonstrate find_last_not_of void find_last_not_ofDemo(string str1, string str2) { // Finds last character in str1 from index 3 which // is not present in str2 string::size_type ch = str1.find_last_not_of(str2, 3); cout << "First unmatched character : " ; cout << str1[ch]; } // Driver code int main() { string str1( "geeKsforgeeks" ); string str2( "GeeksforGeeks" ); cout << "Original String : " << str1 << endl; cout << "String to be looked in : " << str2 << endl; find_last_not_ofDemo(str1, str2); return 0; } |
输出:
Original String : geeKsforgeeksString to be looked in : GeeksforGeeksFirst unmatched character : K
语法3: 搜索最后一个也是或不是C字符串cstr元素的字符。
size_type string::find_last_not_of (const char* cstr) constcstr : Another C-string with the set of charactersto be used in the search.
笔记 该cstr可能不是空指针(null)。
CPP
// CPP code for string::find_last_not_of (const char* cstr) const #include <iostream> using namespace std; // Function to demonstrate find_last_not_of void find_last_not_ofDemo(string str) { // Finds last character in str which // is not present in "geeksforgeeks" string::size_type ch = str.find_last_not_of( "geeksforgeeks" ); cout << "First unmatched character : " ; cout << str[ch]; } // Driver code int main() { string str( "GeeksforGeeks" ); cout << "Original String : " << str << endl; find_last_not_ofDemo(str); return 0; } |
输出:
Original String : GeeksforGeeksFirst unmatched character : G
语法4: 从索引idx中搜索不是C字符串cstr元素的最后一个字符
size_type string::find_last_not_of (const string& str, size_type idx) constcstr : Another C-string with the set of charactersto be used in the search.idx : is the index number from where we have tostart finding first unmatched character.
笔记 该cstr可能不是空指针(null)。
CPP
// CPP code for size_type string:: find_last_not_of // (const char* cstr, size_type idx) const #include <iostream> using namespace std; // Function to demonstrate find_last_not_of void find_last_not_ofDemo(string str) { // Finds last character in str from 5th index which // is not present in "geeksforgeeks" string::size_type ch = str.find_last_not_of( "geeksForgeeks" , 5); cout << "First unmatched character : " ; cout << str[ch]; } // Driver code int main() { string str( "GeeksforGeeks" ); cout << "Original String : " << str << endl; find_last_not_ofDemo(str); return 0; } |
输出:
Original String : GeeksforGeeksFirst unmatched character : f
语法5: 查找str中不等于char c的最后一个字符。
size_type string::find_last_not_of (const char* cstr) constc: Character c with which contents of str are required to be compared.
CPP
// CPP code for size_type string:: find_last_not_of (char c) const #include <iostream> using namespace std; // Function to demonstrate find_last_not_of void find_last_not_ofDemo(string str) { // Finds last character in str which // is not equal to character 'G' string::size_type ch = str.find_last_not_of( 'G' ); cout << "First unmatched character : " ; cout << str[ch]; } // Driver code int main() { string str( "GeeksforGeeks" ); cout << "Original String : " << str << endl; find_last_not_ofDemo(str); return 0; } |
输出:
Original String : GeeksforGeeksFirst unmatched character : s
语法6: 从索引idx中查找str中不等于字符c的最后一个字符。
size_type string::find_last_not_of (const char* cstr, size_type idx) constc: Character c with which contents of str are required to be compared.idx : index from where search of the firstunmatched character is to be started
CPP
// CPP code for size_type string::find_last_not_of // (char c, size_type idx) const #include <iostream> using namespace std; // Function to demonstrate find_last_not_of void find_last_not_ofDemo(string str) { // Finds last character in str from 6th index which // is not equal to character 'G' string::size_type ch = str.find_last_not_of( 'G' , 6); cout << "First unmatched character : " ; cout << str[ch]; } // Driver code int main() { string str( "GeeksforGeeks" ); cout << "Original String : " << str << endl; find_last_not_ofDemo(str); return 0; } |
输出:
Original String : GeeksforGeeksFirst unmatched character : o
语法7: 从索引idx开始,搜索不是字符数组chars的chars_len字符元素的最后一个字符。
size_type string::find_last_not_of (const char* chars, size_type idx, size_typechars_len) const*chars : is the character array with which searching of first unmatched is to be performed.idx : index number in stringchars_len : is the character length in *chars to be picked for searching purpose
CPP
// CPP code for size_type string::find_last_not_of // (const char* chars, size_type idx, size_type chars_len) const #include <iostream> using namespace std; // Function to demonstrate find_first_not_of void find_last_not_ofDemo(string str) { // Finds last character in str from 4th index which // is not equal to any of the first 3 characters from "svmnist" string::size_type ch = str.find_last_not_of( "svmnist" , 4, 3); cout << "First unmatched character : " ; cout << str[ch]; } // Driver code int main() { string str( "GeeksforGeeks" ); cout << "Original String : " << str << endl; find_last_not_ofDemo(str); return 0; } |
输出:
Original String : GeeksforGeeksFirst unmatched character : k
本文由 萨希·提瓦里 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END