strrtrim函数by gipsysoft,顺请Jackzhu拿分
帖一个函数,用起来方便一点:
strrtrim
删除字符串后的空格
char *strrtrim( char *szText );
Return value
The return value is a pointer to the start of the string. The same pointer passed into the function.
用法
char szBuf[] = "test ";
strrtrim( szBuf );
// szBuf now contains the text "test"
函数源码
/*----------------------------------------------------------------------
Copyright (c) 1998,1999 Gipsysoft. All Rights Reserved.
File: strrttrim.cpp
Owner: russf@gipsysoft.com
Web site: http://www.gipsysoft.com
Purpose: strip off any trailing whitespace.
----------------------------------------------------------------------*/
#include "stdafx.h"
#ifndef _INC_CTYPE
#include <ctype.h>
#endif // _INC_CTYPE
extern char * strrtrim( char * pszString );
char * strrtrim( char * pszString )
{
//
// We will only right trim a string if it is not null and it has at least one character
if( pszString && *pszString )
{
//
// Move to the end of the string, as we go check to see if the string is all spaces
// if it is then we record this fact - later we just set the first character to be zero.
char * pszEnd = pszString;
bool bAllSpace = true;
while( *pszEnd )
{
if( bAllSpace && !isspace( *pszEnd ) )
bAllSpace = false;
pszEnd++;
}
if( bAllSpace )
{
*pszString = '\000';
}
else
{
pszEnd--;
//
// Now loop backwards over the string until we get to a non-space character.
// As we go set each space to be zero, this zero terminates
while( ( isspace( *pszEnd ) ) && pszEnd >= pszString )
{
*pszEnd-- = '\000';
}
}
}
return pszString;
}