65,187
社区成员




string& StringTrim(string& str)
{
if (str.empty())
{
return str;
}
// str.erase(0, str.find_first_not_of("\r\n"));
int pos = str.find_first_not_of("\r\n");
if (pos > 0)
{
str.erase(0, pos);
}
while (true)
{
pos = str.find_first_of("\\t");
if (pos > 0)
{
str.erase(0, pos);
continue;
}
break;
}
str.erase(0, str.find_first_not_of("\t"));
str.erase(0, str.find_first_not_of(" "));
str.erase(str.find_last_not_of(" "));
str.erase(0, str.find_first_not_of("\t"));
str.erase(0, str.find_first_not_of(" "));
return str;
}
string s = "\r\n\t \t 京信通信系统(广州)有限公司南京办事处\t\t ";
size_t n = s.find_last_not_of( " \r\n\t" );
if( n != string::npos )
{
s.erase( n + 1 , s.size() - n );
}
n = s.find_first_not_of ( " \r\n\t" );
if( n != string::npos )
{
s.erase( 0 , n );
}
Contents Index Topic Contents
Previous Topic: StrToIntEx
Next Topic: Path Functions
StrTrim
BOOL StrTrim(
LPTSTR psz,
LPCTSTR pszTrimChars
);
Removes (trims) any leading and trailing characters from a string.
Returns nonzero if any characters were removed, or zero otherwise.
psz
Address of a string buffer that contains the string to be trimmed.
pszTrimChars
Address of an array of characters that will be trimmed from psz. The last element in this array must be zero.
Example:
#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"
void main( void )
{
//String one
TCHAR buffer[] = TEXT("_!ABCDEFG#");
TCHAR trim[] = TEXT("#A!_\0");
cout << "The string before calling StrTrim: ";
cout << buffer;
cout << "\n";
StrTrim(buffer, trim);
cout << "The string after calling StrTrim: ";
cout << buffer;
cout << "\n";
}
OUTPUT:
- - - - - -
The string before calling StrTrim: _!ABCDEFG#
The string after calling StrTrim: BCDEFG
--------------------------------------------------------------------------------
Top of Page
© 1997 Microsoft Corporation. All rights reserved. Terms of Use.