一、C++ string包含指定字符串
C++ string類提供了一個成員函數find()來判斷一個字符串中是否包含另一個字符串,如果存在,返回被查找字符串第一次出現的位置,否則返回string::npos。使用例子如下:
#include <string> #include <iostream> using namespace std; int main() { string str1 = "Hello, World!"; string str2 = "World"; if (str1.find(str2) != string::npos) { cout << "String found in position " << str1.find(str2) << endl; } else { cout << "String not found" << endl; } return 0; }
在本例中,我們使用find()來查找str2字符串是否在str1字符串中出現。如果存在,輸出第一次出現的位置;否則輸出「String not found」。該程序的輸出結果為:
String found in position 7
二、string是否包含字符串
除了find()函數之外,我們也可以使用string::compare()函數來判斷一個字符串是否包含另一個字符串,當該函數返回值為0時,表示兩個字符串相等,否則表示不相等。使用示例如下:
#include <string> #include <iostream> using namespace std; int main() { string str1 = "Hello, World!"; string str2 = "World"; if (str1.compare(6, 5, str2) == 0) { cout << "String found!" << endl; } else { cout << "String not found!" << endl; } return 0; }
在本例中,我們使用compare()函數來比較str1的第7個字符開始的5個字符是否等於str2。如果相等,則輸出「String found!」,否則輸出「String not found!」。
三、string截取指定字符串
C++ string類提供了substr()函數來截取一個字符串中的一部分作為新的字符串。使用方式如下:
#include <string> #include <iostream> using namespace std; int main() { string str1 = "Hello, World!"; string str2 = str1.substr(7, 5); cout << "Substring is: " << str2 << endl; return 0; }
在本例中,我們使用substr()函數來截取str1字符串中從第7個字符開始,長度為5個字符的子字符串,並將其賦值給str2。程序的輸出結果為:
Substring is: World
四、string包含某個字符串
我們也可以使用string::find_first_of()函數來查找字符串中是否有某個字符或字符串存在。使用方法如下:
#include <string> #include <iostream> using namespace std; int main() { string str1 = "Hello, World!"; if (str1.find_first_of("Zy") != string::npos) { cout << "Found it!" << endl; } else { cout << "Not found!" << endl; } return 0; }
在本例中,我們使用find_first_of()函數來查找str1中是否包含「Zy」這兩個字符中的任意一個字符,如果存在,則輸出「Found it!」;否則輸出「Not found!」。
五、string字符串截取指定內容
除了使用substr()函數來截取字符串以外,我們還可以使用string::erase()函數來刪除一個字符串中的一部分內容。使用方法如下:
#include <string> #include <iostream> using namespace std; int main() { string str1 = "Hello, World!"; str1.erase(0, 7); cout << "New string is: " << str1 << endl; return 0; }
在本例中,我們使用erase()函數來刪除str1字符串的前7個字符,將得到一個新的字符串。程序輸出的結果為:
New string is: World!
六、string判斷包含字符串
除了使用find()函數來判斷字符串中是否包含某個子字符串以外,我們也可以使用find_first_of()函數來實現同樣功能,例如:
#include <string> #include <iostream> using namespace std; int main() { string str1 = "Hello, World!"; if (str1.find_first_of("World") != string::npos) { cout << "Found it!" << endl; } else { cout << "Not found!" << endl; } return 0; }
在本例中,我們使用find_first_of()函數來查找字符串中是否包含「World」子字符串,如果存在,則輸出「Found it!」;否則輸出「Not found!」。
七、string是否包含某個字符
我們也可以使用find()函數來判斷字符串中是否包含某個字符,例如:
#include <string> #include <iostream> using namespace std; int main() { string str1 = "Hello, World!"; if (str1.find('W') != string::npos) { cout << "Found it!" << endl; } else { cout << "Not found!" << endl; } return 0; }
在本例中,我們使用find()函數來查找字符串中是否包含字符「W」,如果存在,則輸出「Found it!」;否則輸出「Not found!」。
八、string包含字符串的位置
除了使用find()函數來查找字符串中子字符串的位置以外,我們還可以使用string::find_first_of()函數來查找字符串中特定字符的位置,例如:
#include <string> #include <iostream> using namespace std; int main() { string str1 = "Hello, World!"; cout << "Position of 'W' is: " << str1.find_first_of('W') << endl; return 0; }
在本例中,我們使用find_first_of()函數來查找字符串中字符「W」的位置,程序的輸出結果為:
Position of 'W' is: 7
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/183503.html