C++ 문자열 교체

Jmnote (토론 | 기여)님의 2023년 10월 28일 (토) 11:29 판 (새 문서: ==개요== ;C++ 문자열 교체 <syntaxhighlight lang='cpp' run> #include <iostream> using namespace std; void strReplaceOnce(string& s, string const& search, string const& replace...)
(차이) ← 이전 판 | 최신판 (차이) | 다음 판 → (차이)

1 개요

C++ 문자열 교체
#include <iostream>
using namespace std;

void strReplaceOnce(string& s, string const& search, string const& replace) {
    size_t pos = s.find(search);
    if (pos != string::npos) s.replace(pos, search.length(), replace);
}

int main() {
    string s = "hello world hello";
    strReplaceOnce(s, "hello", "yellow");
    cout << s; // yellow world hello
}

2 같이 보기

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}