[c++] C++에서의 문자열에서 특정 부분을 다른 문자열로 치환하는 방법
  1. replace 함수 사용 ```c++ #include #include

int main() { std::string str = “Hello, World!”; str.replace(7, 5, “C++”); // “World”를 “C++”로 치환 std::cout « str « std::endl; // 출력: “Hello, C++!” return 0; }


2. **find와 substr 함수 사용**
```c++
#include <iostream>
#include <string>

int main() {
    std::string str = "Hello, World!";
    size_t pos = str.find("World");  // "World"의 위치를 찾음
    if (pos != std::string::npos) {
        str = str.substr(0, pos) + "C++" + str.substr(pos + 5);  // "World"를 찾아 "C++"로 치환
        std::cout << str << std::endl;  // 출력: "Hello, C++!"
    }
    return 0;
}
  1. 수동으로 치환 ```c++ #include #include

int main() { std::string str = “Hello, World!”; size_t pos = str.find(“World”); // “World”의 위치를 찾음 if (pos != std::string::npos) { str = str.substr(0, pos) + “C++” + str.substr(pos + 5); // “World”를 찾아 “C++”로 치환 std::cout « str « std::endl; // 출력: “Hello, C++!” } return 0; } ```

위의 예제는 C++에서 문자열 내에서 특정 부분을 다른 문자열로 치환하는 세 가지 방법을 보여줍니다. 각각의 방법은 다양한 시나리오에 따라 유용할 수 있습니다.

참고문헌: