Microsoft KB Archive/114590

{|
 * width="100%"|

HOWTO: Removing Spaces from the End of a CString

 * }

Q114590

-

The information in this article applies to:


 * The Microsoft Foundation Classes (MFC), used with:
 * Microsoft Visual C++, versions 1.0, 1.5, 1.51, 1.52

-

SUMMARY
The CString class, which is included in MFC 2.0 and 2.5, does not contain a function that will remove white-space characters from the end of a CString's string. The sample code demonstrates one possible way to write this function.

These steps are not necessary if you are creating a 32-bit application. The 32-bit implementation of CString has member functions TrimRight and TrimLeft, which help you remove leading/trailing blank spaces from the string.

MORE INFORMATION
The following function relies on the current CString interface to work properly. RemEndBlanks was not written to be Double Byte Character Set (DBCS) aware, but it may work properly because the function only checks for white-space characters, which can be neither leading nor trailing bytes. For more information on DBCS, refer to Tech Note #44, which was released with Microsoft Visual C++ for Windows, version 1.5.

Sample Code
/* Compile options needed: NONE void RemEndBlanks(CString &s) { BOOL Done = FALSE; int SLen = s.GetLength; if(SLen) { // Checks for all white space characters for(int Len = SLen-1; Len >= 0 && !Done; --Len) { if(!isspace(s[Len])) Done = TRUE; }   Len++;        // Add 1 to recover from extra decrement in loop if(Len < SLen-1 && Done) // Remove any white space characters s = s.Left(Len+1);    // from the end of the string else if(!Done)          // If string contains only white space s.Empty;            // characters, empty it  } } Additional query words: kbinf 1.00 1.50 2.00 2.50 2.51 2.52
 * 1) include   // required for isspace C run-time function
 * 1) include   // required for isspace C run-time function

Keywords :

Issue type : kbhowto

Technology : kbAudDeveloper kbMFC