當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C++ std::string::find_first_not_of用法及代碼示例


它在字符串中搜索與參數中指定的任何字符都不匹配的第一個字符。在這裏,我們將描述它擁有的所有語法。
返回值:成功時第一個不匹配字符的索引;如果找不到這樣的字符,則返回string::npos。

語法1:搜索不是字符串str元素的第一個字符。

size_type string::find_first_not_of (const string& str) const
str:Another string with the set of characters
to be used in the search.
// CPP code for find_first_not_of (const string& str) const 
    
#include <iostream> 
using namespace std; 
    
// Function to demonstrate find_first_not_of 
void find_first_not_ofDemo(string str1, string str2) 
{ 
    // Finds first character in str1 which 
    // is not present in str2 
    string::size_type ch = str1.find_first_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_first_not_ofDemo(str1, str2); 
    
    return 0; 
}

輸出:


Original String:Hello World!
String to be looked in:GeeksforGeeks
First unmatched character:H

語法2:從索引idx搜索不是字符串str元素的第一個字符。

size_type string::find_first_not_of (const string& str, size_type idx) const
str:Another string with the set of characters
to be used in the search.
idx:is the index number from where we have to
start finding first unmatched character.
// CPP code for string::find_first_not_of 
// (const string& str, size_type idx) const 
  
#include <iostream> 
using namespace std; 
    
// Function to demonstrate find_first_not_of 
void find_first_not_ofDemo(string str1, string str2) 
{ 
    // Finds first character in str1 from index 3 which 
    // is not present in str2 
    string::size_type ch = str1.find_first_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_first_not_ofDemo(str1, str2); 
    
    return 0; 
}

輸出:

Original String:geeKsforgeeks
String to be looked in:GeeksforGeeks
First unmatched character:K

語法3:搜索不是C-string cstr元素的第一個字符。

size_type string::find_first_not_of (const char* cstr) const
cstr:Another C-string with the set of characters
to be used in the search.

注意該cstr可能不是空指針(NULL)。

// CPP code for string::find_first_not_of (const char* cstr) const 
  
#include <iostream> 
using namespace std; 
    
// Function to demonstrate find_first_not_of 
void find_first_not_ofDemo(string str) 
{ 
    // Finds first character in str which 
    // is not present in "geeksforgeeks" 
    string::size_type ch = str.find_first_not_of("geeksforgeeks"); 
    cout << "First unmatched character:"; 
    cout << str[ch]; 
} 
           
// Driver code 
int main() 
{ 
    string str("GeeksforGeeks"); 
   
    cout << "Original String:" << str << endl; 
    find_first_not_ofDemo(str); 
    
    return 0; 
}

輸出:

Original String:GeeksforGeeks
First unmatched character:G

語法4:從索引idx搜索不是C-string cstr元素的第一個字符

size_type string::find_first_not_of (const char* cstr, size_type idx) const
cstr:Another string with the set of characters
to be used in the search.
idx:is the index number from where we have to
start finding first unmatched character.

注意該cstr可能不是空指針(NULL)。

// CPP code for size_type string::find_first_not_of  
// (const char* cstr, size_type idx) const 
  
#include <iostream> 
using namespace std; 
    
// Function to demonstrate find_first_not_of 
void find_first_not_ofDemo(string str) 
{ 
    // Finds first character in str from 5th index which 
    // is not present in "geeksforgeeks" 
    string::size_type ch = str.find_first_not_of("geeksForgeeks", 5); 
    cout << "First unmatched character:"; 
    cout << str[ch]; 
} 
           
// Driver code 
int main() 
{ 
    string str("GeeksforGeeks"); 
   
    cout << "Original String:" << str << endl; 
    find_first_not_ofDemo(str); 
    
    return 0; 
}

輸出:

Original String:GeeksforGeeks
First unmatched character:f

語法5:在str中查找不等於char c的第一個字符。

size_type string::find_first_not_of (char c) const
c Character c with which contents of str are required to be compared.
// CPP code for size_type string::find_first_not_of (char c) const 
  
#include <iostream> 
using namespace std; 
    
// Function to demonstrate find_first_not_of 
void find_first_not_ofDemo(string str) 
{ 
    // Finds first character in str which 
    // is not equal to character 'G' 
    string::size_type ch = str.find_first_not_of('G'); 
    cout << "First unmatched character:"; 
    cout << str[ch]; 
} 
           
// Driver code 
int main() 
{ 
    string str("GeeksforGeeks"); 
   
    cout << "Original String:" << str << endl; 
    find_first_not_ofDemo(str); 
    
    return 0; 
}

輸出:


Original String:GeeksforGeeks
First unmatched character:e

語法6:從索引idx中查找不等於char c的str中的第一個字符。

size_type string::find_first_not_of (char c, size_type idx) const
c: Character c with which contents of str
are required to be compared.
idx: index from where search of the first
unmatched character is to be started
// CPP code for size_type string::find_first_not_of  
// (char c, size_type idx) const 
  
#include <iostream> 
using namespace std; 
    
// Function to demonstrate find_first_not_of 
void find_first_not_ofDemo(string str) 
{ 
    // Finds first character in str from 6th index which 
    // is not equal to character 'G' 
    string::size_type ch = str.find_first_not_of('G', 6); 
    cout << "First unmatched character:"; 
    cout << str[ch]; 
} 
           
// Driver code 
int main() 
{ 
    string str("GeeksforGeeks"); 
   
    cout << "Original String:" << str << endl; 
    find_first_not_ofDemo(str); 
    
    return 0; 
}

輸出:

Original String:GeeksforGeeks
First unmatched character:o

語法7:從索引idx開始,搜索也不是字符數組chars的chars_len字符的元素的第一個字符。

size_type string::find_first_not_of 
(const char* chars, size_type idx, size_type chars_len) const
*chars:is the character array with which 
searching of first unmatched is to be performed.
idx: index number in string
chars_len:is the character length in 
*chars to be picked for searching purpose
// CPP code for size_type string::find_first_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_first_not_ofDemo(string str) 
{ 
    // Finds first 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_first_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_first_not_ofDemo(str); 
    
    return 0; 
}

輸出:

Original String:GeeksforGeeks
First unmatched character:f


相關用法


注:本文由純淨天空篩選整理自 std::string::find_first_not_of in C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。