一文帶你了解C++中的字符替換方法
一、單個字符替換
1.1 std::replace
代碼示例:
#include <algorithm> // ... std::string str = "Hello, World!"; std::replace(str.begin(), str.end(), 'o', 'O'); // str 現(xiàn)在為 "HellO, WOrld!"
1.2 使用循環(huán)手動替換
std::string str = "Hello, World!";
for (char& c : str) {
if (c == 'o') {
c = 'O';
}
}
// str 現(xiàn)在為 "HellO, WOrld!"
1.3 使用正則表達式庫(例如,std::regex_replace)
#include <regex>
// ...
std::string str = "Hello, World!";
std::string result = std::regex_replace(str, std::regex("o"), "O");
// result 現(xiàn)在為 "HellO, WOrld!"
二、字符串替換
2.1 實用字符串流
可以使用 C++ 的字符串流(stringstream)來實現(xiàn)字符串的替換。下面是一個例子:
#include <sstream>
#include <string>
std::string replace(std::string str, const std::string& from, const std::string& to) {
std::stringstream ss;
size_t start_pos = 0;
while ((start_pos = str.find(from, start_pos)) != std::string::npos) {
ss << str.substr(0, start_pos) << to;
start_pos += from.length();
str = str.substr(start_pos);
}
ss << str;
return ss.str();
}
int main() {
std::string str = "Hello, World!";
std::string from = "World";
std::string to = "C++";
std::string result = replace(str, from, to);
std::cout << result << std::endl; // 輸出:Hello, C++!
return 0;
}2.2 使用字符數(shù)組
也可以使用字符數(shù)組來實現(xiàn)字符串的替換。下面是一個例子:
#include <iostream>
#include <cstring>
char* replace(char* str, const char* from, const char* to) {
size_t str_len = strlen(str);
size_t from_len = strlen(from);
size_t to_len = strlen(to);
size_t new_len = str_len + to_len - from_len;
char* new_str = new char[new_len + 1];
char* p1 = str;
char* p2 = new_str;
while (*p1) {
if (strncmp(p1, from, from_len) == 0) {
strncpy(p2, to, to_len);
p2 += to_len;
p1 += from_len;
} else {
*p2++ = *p1++;
}
}
*p2 = '\0';
delete[] str;
return new_str;
}
int main() {
char str[] = "Hello, World!";
const char* from ="World";
const char* to = "C++";
char* result = replace(str, from, to);
std::cout << result << std::endl; // 輸出:Hello, C++!
delete[] result;
return 0;
}2.3 使用 STL 的算法:std::replace
下面是使用 STL 的 `replace` 算法來實現(xiàn)字符串的替換的例子:
#include <algorithm>
#include <string>
std::string replace(std::string str, const std::string& from, const std::string& to) {
size_t start_pos = 0;
while ((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length();
}
return str;
}
int main() {
std::string str = "Hello, World!";
std::string from = "World";
std::string to = "C++";
std::string result = replace(str, from, to);
std::cout << result << std::endl; // 輸出:Hello, C++!
return 0;
}2.4 使用正則表達式
如果要替換字符串中的多個子串,或者要進行更復雜的字符串替換操作,可以使用正則表達式。下面是使用 C++ 的正則表達式庫 <regex> 來實現(xiàn)字符串的替換的例子:
#include <regex>
#include <string>
std::string replace(std::string str, const std::string& pattern, const std::string& to) {
std::regex r(pattern);
return std::regex_replace(str, r, to);
}
int main() {
std::string str = "Hello, World!";
std::string str = "Hello, World!";
std::string result = replace(str, "World", "C++"); // 替換所有的 "World" 為 "C++"
std::cout << result << std::endl; // 輸出:Hello, C++!
result = replace(str, "[Ww]orld", "C++"); // 替換所有的 "World" 或 "world" 為 "C++"
std::cout << result << std::endl; // 輸出:Hello, C++!
result = replace(str, "[a-zA-Z]+", "C++"); // 替換所有的單詞為 "C++"
std::cout << result << std::endl; // 輸出:C++, C++!
result = replace(str, "\\b\\w+\\b", "C++"); // 同上
std::cout << result << std::endl; // 輸出:C++, C++!
}注意:在使用正則表達式時,需要在字符串中的正則表達式前面加上 "\\"
三、總結
在 C++ 中,可以使用字符串流、字符數(shù)組、STL 的算法、正則表達式等方法來實現(xiàn)字符串的替換。根據(jù)實際需要,可以選擇適合的方法來實現(xiàn)字符串的替換。
到此這篇關于一文帶你了解C++中的字符替換方法的文章就介紹到這了,更多相關C++字符替換內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
OpenCV4 實現(xiàn)背景分離的詳細步驟(背景減法模型)
背景分離(BS)是一種通過使用靜態(tài)相機來生成前景掩碼(即包含屬于場景中的移動對象像素的二進制圖像)的常用技術,本文給大家介紹OpenCV4 實現(xiàn)背景分離的詳細步驟,需要的朋友可以參考下2021-09-09
用標準c++實現(xiàn)string與各種類型之間的轉換
這個類在頭文件中定義, < sstream>庫定義了三種類:istringstream、ostringstream和stringstream,分別用來進行流的輸入、輸出和輸入輸出操作。另外,每個類都有一個對應的寬字符集版本2013-09-09
用C語言求冪函數(shù)和指數(shù)函數(shù)的方法
這篇文章主要介紹了用C語言求冪函數(shù)和指數(shù)函數(shù)的方法,即pow()函數(shù)和sqrt()函數(shù)的使用,需要的朋友可以參考下2015-08-08

