types.h
#ifndef _Types_H_
#define _Types_H_
#include <string>
#ifndef UNICODE
typedef std::string String;
#else
typedef std::wstring String;
#endif
#endif // _Types_H_
text.h
#ifndef _Text_H_
#define _Text_H_
#include "types.h"
// trim from start
extern String <rim(String &s);
// trim from end
extern String &rtrim(String &s);
// trim from both ends
extern inline String &trim(String &s);
#endif // _Text_H_
text.cpp
#include <algorithm>
#include <functional>
#include <cctype>
#include <locale>
#include "text.h"
// trim from start
inline String <rim(String &s)
{
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
return s;
}
// trim from end
inline String &rtrim(String &s)
{
s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
return s;
}
// trim from both ends
inline String &trim(String &s)
{
return ltrim(rtrim(s));
}
以上为相关代码。