Here's another version:
void Trim(std::string& str, const std::string & ChrsToTrim = " \t\n\r", int TrimDir = 0)
{
size_t startIndex = str.find_first_not_of(Chrs
if (startIndex == std::string::npos){str.era
if (TrimDir < 2) str = str.substr(startIndex, str.size()-startIndex);
if (TrimDir!=1) str = str.substr(0, str.find_last_not_of(ChrsT
}
inline void TrimRight(std::string& str, const std::string & ChrsToTrim = " \t\n\r")
{
Trim(str, ChrsToTrim, 2);
}
inline void TrimLeft(std::string& str, const std::string & ChrsToTrim = " \t\n\r")
{
Trim(str, ChrsToTrim, 1);
}
Main Topics
Browse All Topics





by: AxterPosted on 2005-01-20 at 13:56:25ID: 13097996
Here's some trim code;
espace);
space);
string ltrim( const string &str, const string &whitespace = "\t ")
{
int idx = str.find_first_not_of(whit
if( idx != string::npos )
return str.substr(idx);
return "";
}
string rtrim( const string &str, const string &whitespace = "\t ")
{
int idx = str.find_last_not_of(white
if( idx != string::npos )
return str.substr(0,idx+1);
return str;
}
string trim( const string &str, const string &whitespace = "\t ")
{
return rtrim(ltrim(str));
}
void main()
{
string str = " \t Hello sky ";
cout << "data >" << str << "***" << endl ;
cout << "left trim >" << ltrim(str) << "***" << endl ;
cout << "right trim >" << rtrim(str) << "***" << endl ;
cout << "trim >" << trim(str) << "***" << endl ;
}