wxString中的Trim函数

19 views
Skip to first unread message

xunj...@gmail.com

unread,
Apr 10, 2006, 10:05:17 PM4/10/06
to wxLife
根据手册中的说明:
wxString::Trim
wxString& Trim(bool fromRight = true)
Removes spaces from the left or from the right (default).
该函数应该是根据参数消除字符串左侧或者右侧的空格。但这次在实际项目中用MinGW编译时发现,该函数会把中文字符也作为空格删除。所以,只能自己写消除纯空格的函数了。

inline bool IsSpace( IN wxChar ch )
{
return ch == wxT(' ');
}

inline wxString TrimLeft( IN const wxString& str )
{
const wxChar * pszStr = str.c_str();
for ( int index = (int) str.Length() - 1; index >= 0; index -- )
{
if ( ! IsSpace( pszStr[index] ) )
{
return str.Left( index + 1 );
}
}
return wxEmptyString;
}

inline wxString TrimRight( IN const wxString& str )
{
const wxChar * pszStr = str.c_str();
for ( int index = 0; index < (int) str.Length(); index ++ )
{
if ( ! IsSpace( pszStr[index] ) )
{
return str.Right( str.Length() - index );
}
}
return wxEmptyString;
}

inline wxString TrimBoth( IN const wxString& str )
{
return TrimLeft( TrimRight( str ) );
}

Reply all
Reply to author
Forward
0 new messages