一、字元串的定義和常用操作
C++中的字元串可以使用char數組或者string類實現。char數組是一組字元的集合,可以用字元串字面量或者字元數組初始化。string類則是一個預定義的類,可以直接使用字元串字面量或者字元串進行初始化。以下是常用的字元串操作函數:
#include <iostream>
#include <string>
int main() {
char str1[] = "Hello";
char str2[] = {'w', 'o', 'r', 'l', 'd', '\0'};
std::string str3 = "C++";
std::string str4 = "Programming";
// 獲取字元串長度
std::cout << "Length of str1: " << strlen(str1) << std::endl;
std::cout << "Length of str2: " << strlen(str2) << std::endl;
std::cout << "Length of str3: " << str3.length() << std::endl;
std::cout << "Length of str4: " << str4.size() << std::endl;
// 字元串拼接
std::cout << "Concatenation of str1 and str2: " << strcat(str1, str2) << std::endl;
std::cout << "Addition of str3 and str4: " << str3 + " " + str4 << std::endl;
// 字元串複製
char str5[10];
strcpy(str5, str1);
std::cout << "Copy of str1: " << str5 << std::endl;
// 字元串查找
std::cout << "Index of 'o' in str1: " << strchr(str1, 'o') - str1 + 1 << std::endl;
std::cout << "Last index of 'l' in str1: " << strrchr(str1, 'l') - str1 + 1 << std::endl;
std::cout << "Index of 'PP' in str3: " << str3.find("PP") + 1 << std::endl;
return 0;
}
二、字元串的遍歷和比較
在進行字元串遍歷時,可以使用循環結構進行逐個遍歷,也可以使用string類的迭代器進行遍歷。字元串的比較可以使用比較運算符進行比較,也可以使用字元串操作函數進行比較。
#include <iostream>
#include <string>
int main() {
std::string str1 = "This is a C++ string";
std::string str2 = "C++ is a high-levle programming language";
std::string str3 = "The quick brown fox jumps over the lazy dog";
std::string str4 = "The quick brown fox jumps over the lazy dog";
// 字元串遍歷 - 循環
for (int i = 0; i < str1.length(); i++) {
std::cout << str1[i] << " ";
}
std::cout << std::endl;
// 字元串遍歷 - 迭代器
for (std::string::iterator it = str2.begin(); it != str2.end(); it++) {
std::cout << *it << "-";
}
std::cout << std::endl;
// 字元串比較
if (str3 == str4) {
std::cout << "str3 is equal to str4" << std::endl;
} else {
std::cout << "str3 is not equal to str4" << std::endl;
}
if (str1.compare(str2) == 0) {
std::cout << "str1 is equal to str2" << std::endl;
} else {
std::cout << "str1 is not equal to str2" << std::endl;
}
return 0;
}
三、字元串的截取和分割
在進行字元串截取時,可以使用substr方法,該方法接受兩個參數,第一個參數是開始截取的位置,第二個參數是截取的長度。在進行字元串分割時,可以使用getline方法,該方法接受兩個參數,第一個參數是輸入流,第二個參數是分隔符。通過循環使用getline方法,可以將字元串分割成多個子字元串。
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
int main() {
std::string str1 = "C++ is a high-level programming language";
std::string str2 = "red,green,blue,yellow,black";
// 字元串截取
std::cout << "Substring of str1 from the 9th character for 5 characters: " << str1.substr(9, 5) << std::endl;
std::cout << "Substring of str1 from the 0th character for the first space character: " << str1.substr(0, str1.find(" ")) << std::endl;
// 字元串分割
std::stringstream ss(str2);
std::vector<std::string> vec;
std::string temp;
char delimiter = ',';
while (getline(ss, temp, delimiter)) {
vec.push_back(temp);
}
std::cout << "The original string: " << str2 << std::endl;
std::cout << "The vector of substrings: ";
for (auto x : vec) {
std::cout << x << " ";
}
std::cout << std::endl;
return 0;
}
四、字元串的插入和刪除
字元串的插入可以使用insert方法,該方法接受兩個參數,第一個參數是插入的位置,第二個參數是插入的字元串。字元串的刪除可以使用erase方法,該方法接受兩個參數,第一個參數是刪除的起始位置,第二個參數是刪除的長度。
#include <iostream>
#include <string>
int main() {
std::string str1 = "This is a C++ string";
std::string str2 = "Programming";
std::string str3 = "C";
// 字元串插入
std::cout << "Inserting 'great' at position 10: " << str1.insert(10, " great") << std::endl;
// 字元串刪除
std::cout << "Erasing 7 characters starting from position 8: " << str2.erase(8, 7) << std::endl;
// 刪除指定字元
str3.erase(std::remove(str3.begin(), str3.end(), 'C'), str3.end());
std::cout << "Removing all 'C' from str3: " << str3 << std::endl;
return 0;
}
五、字元串的轉換和格式化
在進行字元串轉換時,可以使用atoi、atof等函數將字元串轉換成數字、浮點數等類型。在進行字元串格式化時,可以使用sprintf函數和C++11後的std::to_string方法進行格式化。
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iomanip>
int main() {
char str1[] = "12345";
char str2[] = "3.1415";
std::string str3 = "35";
std::string str4 = "Hello";
int num1 = atoi(str1);
double num2 = atof(str2);
int num3 = std::stoi(str3);
double num4 = std::stod(str2);
// 字元串轉換
std::cout << "Converting str1 to an integer: " << num1 << std::endl;
std::cout << "Converting str2 to a double: " << num2 << std::endl;
std::cout << "Converting str3 to an integer: " << num3 << std::endl;
std::cout << "Converting str2 to a double: " << num4 << std::endl;
// 字元串格式化
char buffer[50];
sprintf(buffer, "%-.4f", num2);
std::cout << "Formatting num2 to 4 decimal places: " << buffer << std::endl;
std::string str5 = std::to_string(num2);
std::cout << "Formatting num2 to 2 decimal places: " << std::setprecision(2) << std::fixed << num2 << std::endl;
return 0;
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/159969.html