一、string的定義
在C++中,string是一個類,它是STL的一部分,因此 string是標準模板庫中的一個模板類,可以通過使用該模板類輕鬆操作字元串。在C++中,string是動態的,這意味著它可以自動調整大小以適應不同的字元串長度。
//string的定義
#include
#include
using namespace std;
int main(){
string str1 = "Hello";
string str2 = "World";
string str3 = str1 + str2;
cout << str3 << endl;
return 0;
}
二、string的常用方法
string提供了許多有用的方法,用於操作和處理字元串。在這裡我們列出一些常用的方法:
1、length(),size()
這兩個方法用於返回字元串的長度。
//length()和size()的演示
#include
#include
using namespace std;
int main(){
string str = "Hello World!";
cout << "length of string str: " << str.length() << endl;
cout << "size of string str: " << str.size() << endl;
return 0;
}
2、compare()
該方法用於比較兩個字元串的大小。當字元串相等時返回0,str1>str2時返回大於0的值,str1<str2時返回小於0的值。
//compare()的演示
#include
#include
using namespace std;
int main(){
string str1 = "Hello";
string str2 = "hello";
if (str1.compare(str2) == 0) {
cout << "Strings are equal." < 0) {
cout << "str1 is greater than str2." << endl;
} else {
cout << "str2 is greater than str1." << endl;
}
return 0;
}
3、substr()
該方法用於返回字元串的一個子串。
//substr()的演示
#include
#include
using namespace std;
int main(){
string str = "Hello World!";
string subStr = str.substr(6, 5);
cout << "Substring is: " << subStr << endl;
return 0;
}
4、find()
該方法用於查找字元串中第一次出現子串的位置。
//find()的演示
#include
#include
using namespace std;
int main(){
string str = "Hello World!";
size_t found = str.find("World");
if (found != string::npos) {
cout << "Substring found at position: " << found << endl;
} else {
cout << "Substring not found." << endl;
}
return 0;
}
三、string的容器特性
因為string是一個類,它具有內部的容器特性,這些特性可以用來存儲其值,同時許多STL演算法也可以使用string容器執行操作。
1、push_back()
可以使用該方法向string容器中添加字元。
//push_back()的演示
#include
#include
using namespace std;
int main(){
string str = "Hello";
str.push_back(' ');
str.push_back('W');
str.push_back('o');
str.push_back('r');
str.push_back('l');
str.push_back('d');
cout << str << endl;
return 0;
}
2、clear()
該方法清空容器中的字元串。
//clear()的演示
#include
#include
using namespace std;
int main(){
string str = "Hello World!";
cout << "Before clear: " << str << endl;
str.clear();
cout << "After clear: " << str << endl;
return 0;
}
3、empty()
該方法用於檢查一個字元串容器是否為空。
//empty()的演示
#include
#include
using namespace std;
int main(){
string str1 = "";
string str2 = "Hello World!";
if (str1.empty()) {
cout << "str1 is empty." << endl;
} else {
cout << "str1 is not empty." << endl;
}
if (str2.empty()) {
cout << "str2 is empty." << endl;
} else {
cout << "str2 is not empty." << endl;
}
return 0;
}
四、string的異常處理
string類也提供了多種異常處理方法,以防止程序在執行過程中出現崩潰的情況。
1、at()
at()方法用於訪問下標,當訪問到一個不存在的下標時,該方法會拋出out_of_range異常。
//at()的演示
#include
#include
using namespace std;
int main(){
string str = "Hello World!";
try {
char ch = str.at(20);
} catch (std::out_of_range& e) {
cout << "Exception: " << e.what() << endl;
}
return 0;
}
2、erase()
該方法用於刪除一個字元串中的一部分內容。
//erase()的演示
#include
#include
using namespace std;
int main(){
string str = "Hello World!";
try {
str.erase(6, 6);
cout << str << endl;
str.erase(11, 1);
cout << str << endl;
} catch (std::out_of_range& e) {
cout << "Exception: " << e.what() << endl;
}
return 0;
}
3、replace()
該方法用於替換一個字元串中的一部分內容。
//replace()的演示
#include
#include
using namespace std;
int main(){
string str = "Hello World!";
try {
str.replace(6, 5, "C++");
cout << str << endl;
} catch (std::out_of_range& e) {
cout << "Exception: " << e.what() << endl;
}
return 0;
}
總結
在C++編程中,字元串是一種十分常見且基礎的數據類型,對於熟練掌握string的開發人員來說,C++的編程難度將會大大降低。本文深入探究了string在C++中的定義、常用方法以及容器特性和異常處理方法,相信這些知識對於初學者是非常有幫助的。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/252150.html