std::string의 ReplaceAll 함수
요즘은 CString을 ATL에서 사용가능하게 되면서 std::string을 사용할 일이 줄어들긴 했지만, 여전히 유용한 클래스임에는 틀림없다. (특히나 windows개발이 아니라면)
여튼. std::string에서 아쉬운 부분중에 하나인 ReplaceAll 함수를 간단히 구현해 보자.
(물론 iterator등을 이용하는게 정석 이겠지만, 귀찮으니까 -_-)
- #include <string>
- typedef std::basic_string<TCHAR> _tstring;
- _tstring string_replace_all(
- const _tstring& source,
- const _tstring& pattern,
- const _tstring& replace )
- {
- _tstring result = source;
- _tstring::size_type pos = 0;
- _tstring::size_type offset = 0;
- _tstring::size_type pattern_len = pattern.size();
- _tstring::size_type replace_len = replace.size();
- while ( ( pos = result.find( pattern, offset ) ) != _tstring::npos )
- {
- result.replace( result.begin() + pos,
- result.begin() + pos + pattern_len,
- replace );
- offset = pos + replace_len;
- }
- return result;
- }
'소프트웨어 개발' 카테고리의 다른 글
| 파일을 클립보드로 복사하기 (5) | 2009/04/06 |
|---|---|
| Connection String이 필요할땐? (5) | 2009/04/03 |
| std::string의 ReplaceAll 함수 (2) | 2009/04/02 |
| File Size 출력 - StrFormatByteSize (4) | 2009/04/01 |
| eclipse 맞춤법 검사 끄기 (5) | 2009/03/31 |
| Windows Shell Path Handling API (7) | 2009/03/31 |

Tag //

Comment List
좋은 기사 감사합니다
티끌모아 태산