CString::TrimLeft
void TrimLeft( );当在没有参数的情况下调用时,TrimLeft删除字符串最前面的换行符,空格和tab字符。
void CString::TrimLeft( TCHAR chTarget );
void CString::TrimLeft( LPCTSTR lpszTargets );
Parameters
chTarget
The target characters to be trimmed.
lpszTargets
A pointer to a string containing the target characters to be trimmed.
Remarks
Call the version of this member function with no parameters to trim leading whitespace characters from the string. When used with no parameters, TrimLeft removes newline, space, and tab characters.
Use the versions of this function that accept parameters to remove a particular character or a particular group of characters from the beginning of a string.
For more information, seeStrings Topics in Visual C++ Programmer’s Guide
Example
In this example, the string "\t\t ****Hockey is best!" becomes "Hockey is best!":
CString strBefore; CString strAfter; strBefore = _T("\t\t ****Hockey is best!"); strAfter = strBefore; strAfter.TrimLeft(T_("\t *"));用来将一个特定的字符或一群特定的字符从字符串的开始处删除。 _tprintf(_T("Before: \"%s\"\n"), (LPCTSTR) strBefore);
_tprintf(_T("After : \"%s\"\n"), (LPCTSTR) strAfter);
CString::TrimRight
void TrimRight( );当在没有参数的情况下调用时,消除从右侧起所遇到的所有空格字符。
void CString::TrimRight( TCHAR chTarget );
void CString::TrimRight( LPCTSTR lpszTargets );
Parameters
chTarget
The target characters to be trimmed.
lpszTargets
A pointer to a string containing the target characters to be trimmed.
Remarks
Call the version of this member function with no parameters to trim trailing whitespace characters from the string. When used with no parameters, TrimRight removes trailing newline, space, and tab characters from the string.
Use the versions of this function that accept parameters to remove a particular character or a particular group of characters from the end of a string.
Example
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".