Trims trailing characters from the string.

CStringT& TrimRight(
   XCHAR chTarget 
CStringT& TrimRight(
   PCXSTR pszTargets 
CStringT& TrimRight( );


The target character to be trimmed.


A pointer to a string containing the target characters to be trimmed. All trailing occurrences of characters in pszTarget will be trimmed from the CStringT object.

Returns the CStringT object that contains the trimmed string.

Removes trailing occurrences of one of the following:

  • The character specified by chTarget.

  • All characters found in the string specified by pszTargets.

  • Whitespace.

The CStringT& TrimRight(XCHAR chTarget) version accepts one character parameter and removes all copies of that character from the end of CStringT string data. It starts from the end of the string and works toward the front. It stops when it finds a different character or when CSTringT runs out of character data.

The CStringT& TrimRight(PCXSTR pszTargets) version accepts a null-terminated string that contains all the different characters to search for. It removes all copies of those characters in the CStringT object. It starts at the end of the string and works toward the front. It stops when it finds a character that is not in the target string, or when CStringT runs out of character data. It does not try to match the whole target string to a substring at the end of CStringT.

The CStringT& TrimRight() version requires no parameters. It trims any trailing whitespace characters from the end of the CStringT string. Whitespace characters can be line breaks, spaces, or tabs.

// typedef CStringT<TCHAR, StrTraitATL<TCHAR, ChTraitsCRT<TCHAR>>> CAtlString;

CAtlString str;
str = _T("Soccer is best!?!?!?!?!");

_tprintf_s(_T("Before: \"%s\"\n"), (LPCTSTR)str);
_tprintf_s(_T("After : \"%s\"\n"), (LPCTSTR)str.TrimRight(_T("?!")));

// Output: 
// -------------------------- 
// Before: Soccer is best!?!?!?!?! 
// After: Soccer is best

The output from this example is as follows:

Before: "Soccer is best!?!?!?!?!"

After : "Soccer is best"

Header: cstringt.h