G
Generic Usenet Account
I have worked out the following implementation for trimming the leading
and trailing whitespace characters of a string (I am surprised not to
see these as member functions). Am I doing it correctly, or is there a
better way to do it?
Thanks,
Gus
//////////// Source code begin /////////////////////
#include <iostream>
#include <string>
using namespace std;
void
trimLeading(string& s)
{
s = s.substr(s.find_first_not_of(" \t"));
}
void
trimTrailing(string& s)
{
s = s.substr(0, s.find_last_not_of(" \t")+1);
}
main()
{
string str1 = " Here's my sample string ";
string str2 = str1;
trimLeading(str1);
trimTrailing(str2);
cout << "Output of trimLeading is [" << str1 << "]\n";
cout << "Output of trimTrailing is [" << str2 << "]\n";
}
and trailing whitespace characters of a string (I am surprised not to
see these as member functions). Am I doing it correctly, or is there a
better way to do it?
Thanks,
Gus
//////////// Source code begin /////////////////////
#include <iostream>
#include <string>
using namespace std;
void
trimLeading(string& s)
{
s = s.substr(s.find_first_not_of(" \t"));
}
void
trimTrailing(string& s)
{
s = s.substr(0, s.find_last_not_of(" \t")+1);
}
main()
{
string str1 = " Here's my sample string ";
string str2 = str1;
trimLeading(str1);
trimTrailing(str2);
cout << "Output of trimLeading is [" << str1 << "]\n";
cout << "Output of trimTrailing is [" << str2 << "]\n";
}