溫馨提示×

c++ string怎么替換某字符為其他字符

c++
小億
461
2024-08-17 11:13:34
欄目: 編程語言

要替換C++中的字符串中的某個字符為另一個字符,可以使用std::replace函數。

以下是一個簡單的示例代碼:

#include <iostream>
#include <string>
#include <algorithm>

int main() {
    std::string str = "Hello, World!";
    
    // 將字符串中的逗號(,)替換為感嘆號(!)
    std::replace(str.begin(), str.end(), ',', '!');

    std::cout << str << std::endl;

    return 0;
}

在上面的示例中,我們使用std::replace函數將字符串str中的逗號,替換為感嘆號!。您可以根據需要替換任何字符。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女