字符串NDS_trim.h


#ifndef _NDS_TRIM_H
#define _NDS_TRIM_H

#include <string>
#include <algorithm>
#include <functional>
#include <cctype>
using namespace std;

namespace NDS
{

inline string& ltrim(string &ss, int (*pf)(int)=isspace)
{
   string::iterator p=find_if(ss.begin(),ss.end(),not1(ptr_fun(pf)));
   ss.erase(ss.begin(),p);
   return ss;
}

inline string& rtrim(string &ss, int (*pf)(int)=isspace)
{
   string::reverse_iterator p=find_if(ss.rbegin(),ss.rend(),not1(ptr_fun(pf)));
   ss.erase(p.base(),ss.end());
   return ss;
}

inline string& trim(string &st)
{
   ltrim(rtrim(st));
   return st;
}

inline void stringupper(string& str)
{
   for (string::iterator i=str.begin();i!=str.end();i++)
       *i=toupper(*i);
}

inline void stringlower(string& str)
{
   for (string::iterator i=str.begin();i!=str.end();i++)
       *i=tolower(*i);
}

template <typename T> inline const string to_string(const T& v)
{
   ostringstream os;
   os<<v;
   return os.str();
}

template <typename T> inline const T from_string(const string& v)
{
   istringstream is(v);
   T t;
   is>>t;
   return t;
}

template <typename T> inline void from_string(T& t, const string& v)
{
   istringstream is(v);
   is>>t;
}

}

#endif

对字符串string去头尾空白字符的操作,新增加了字符串转换成T类型的操作函数。
有一点尚未解决,是任意类型转成字符串时的格式化问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值