방법 | 사용 | 목적코드 |
str.replace (위치, 길이, 새 문자열) |
특정 위치의 문자열을 변경 | str.replace(6, 5, "C++"); |
std::regex_replace() | 여러 개의 특정 문자열 변경 (정규식 가능) |
std::regex_replace(str, std::regex("apple"), "banana") |
std::replace() | 특정 문자 변경 | std::replace(str.begin(), str.end(), 'a', 'X'); |
std::string::find() + replace() | 문자열 전체 변경 (반복) | while ((pos = str.find(...)) != std::string::npos) str.replace(...); |
std::erase() + std::remove() | 특정 문자 제거 | str.erase(std::remove(str.begin(), str.end(), ' '), str.end()); |
추천 방법
- 특정 단어 1회 치환 → str.replace(위치, 길이, 새 문자열)
- 특정 단어 여러 개 치환 → find() + replace() 반복
- 특정 문자 바꾸기 → std::replace()
- 정규표현식 활용 → std::regex_replace()
- 특정 문자 제거 → std::erase() + std::remove()
728x90
'공부 > C++, C' 카테고리의 다른 글
C++ - stack, que, deque, priority_que (0) | 2025.02.22 |
---|---|
C++ 자료형과 변수 (0) | 2025.02.21 |
메크로 있는지 확인 하기, 선언하기, 해제하기 - #ifdef, #define, #undef (0) | 2024.10.21 |
메크로란? 정의하기, 해제하기 - #define, #undef (0) | 2024.10.21 |
C++, C 공부 시작 (0) | 2024.10.21 |