一、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-tw/n/183503.html