trim
trim is a PHP function to remove spaces around the string. This is a C++ version.
typedef std::basic_string<TCHAR> tstring;
void trim(tstring &str)
{
TCHAR space = _T(' ');
int pos1 = str.find_first_not_of(space);
int pos2 = str.find_last_not_of(space);
if (pos1 == tstring::npos)
pos1 = 0;
if (pos2 == tstring::npos)
pos2 = str.length() - 1;
else
pos2 = pos2 - pos1 + 1;
str = str.substr(pos1, pos2);
}
Arguments
- str
- The string to be trimmed (tstring is a typedef to handle both ansi and unicode strings)
Example
tstring string = _T(" hello world ");
trim(string);
Comments
No comments yet.
Script by Alex
This page was last modified
34 months, 3 weeks, 2 days and 9 hours ago