CString::TrimLeft和CString::TrimRight

本文介绍了如何使用CString类的TrimLeft方法去除字符串开头的特定字符或空白字符,包括无参数版本和指定目标字符版本的用法。示例中展示了TrimLeft如何将"\t\t\t \*\*\*Hockey is best!"转换为"Hockey is best!"。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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".


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值