확장자를 제거하기 위해 TrimRight를 쓰는 경우가 있습니다.
CString Filter = ".txt";
CString szFileName = "MyText.txt";
szFileName.TrimRight(Filter);
작성자가 원하는 결과는 MyText
실제 결과는 MyTex
TrimRight는 Parameter로 받는 글자와 완벽히 일치하는 문자를 제거하는 것이 아니라
Parameter로 받은 글자가 전부 포함된 문자를 제거합니다.
MSDN에 있는 예제를 보면 이해가 빠릅니다.
CString strBefore;
CString strAfter;
strBefore = "Hockey is Best!!!!";
strAfter = strBefore;
str.TrimRight('!');
printf("Before: \"%s\"\n", (LPCTSTR) strBefore);
printf("After : \"%s\"\n\n", (LPCTSTR) strAfter);
strBefore = "Hockey is Best?!?!?!?!";
strAfter = strBefore;
str.TrimRight("?!");
printf("Before: \"%s\"\n", (LPCTSTR) strBefore);
printf("After : \"%s\"\n\n", (LPCTSTR) strAfter);
In the first example above, the string reading, "Hockey is Best!!!!" becomes "Hockey is Best".
In the second example above, the string reading, , "Hockey is Best?!?!?!?!" becomes "Hockey is Best".
<from MSDN : http://msdn.microsoft.com/en-us/library/aa300669(VS.60).aspx>
CString strAfter;
strBefore = "Hockey is Best!!!!";
strAfter = strBefore;
str.TrimRight('!');
printf("Before: \"%s\"\n", (LPCTSTR) strBefore);
printf("After : \"%s\"\n\n", (LPCTSTR) strAfter);
strBefore = "Hockey is Best?!?!?!?!";
strAfter = strBefore;
str.TrimRight("?!");
printf("Before: \"%s\"\n", (LPCTSTR) strBefore);
printf("After : \"%s\"\n\n", (LPCTSTR) strAfter);
In the first example above, the string reading, "Hockey is Best!!!!" becomes "Hockey is Best".
In the second example above, the string reading, , "Hockey is Best?!?!?!?!" becomes "Hockey is Best".
<from MSDN : http://msdn.microsoft.com/en-us/library/aa300669(VS.60).aspx>
첫번째 예제의 느낌표는 모두 사라졌고 두 번째 예제에서는 ?! 가 모두 사라졌습니다. 그렇지만 TrimRight에 넘어가는 Parameter는 "!"와 "?!" 뿐입니다.
함수 이름만 보고는 오해를 불러 일으키기 쉬운 함수인 듯 합니다.
참고로 확장자를 제거하기 위해서는 아래 방법이 제일 무난합니다.
CString Filter = ".txt";
CString szFileName = "MyText.txt";
szFileName = szFileName.Left(szFileName.GetLength - Filter.GetLength());
'class CKnowledge > ....: Programming' 카테고리의 다른 글
| strcpy, sscanf, sprintf 대신하여 안전한 프로그램 짜기 (0) | 2009/05/14 |
|---|---|
| sizeof 몇 가지 (0) | 2009/04/22 |
| CString의 TrimRight에 대한 착각 (0) | 2009/04/09 |
| C++에서 다음 두 코드의 차이는? (0) | 2009/04/03 |
| Google Data Python Library 시작하기 (0) | 2008/10/20 |
| 파이썬(Python) 리스트 내용 출력하기 (0) | 2008/09/25 |