造轮子之常用函数的封装

常用函数的封装

常用函数的封装:

  • 字符串操作常用函数
    • 大小写转换函数
    • 分割字符串函数
    • 字符串转换函数
    • 字符串替换函数
  • 时间操作常用函数
帮助类定义
字符串操作封装
class YR_Common
{
public:
    //去掉头部及尾部的字符或者字符串
    //bool为true 去除s中的每个字符,为false去除s字符串
    static string trim(const string& sStr, const string& s=" \r\n\t", bool bChar=true);
    //去掉左边的字符或者字符串
    static string trimleft(const string &sStr, const string &s = " \r\n\t", bool bChar = true);
    //去掉右边的字符或者字符串
    static string trimright(const string &sStr, const string &s = " \r\n\t", bool bChar = true);
    //字符串转换成小写
    static string lower(const string &sString);
    //字符串转换成大写
    static string upper(const string &sString);
    //字符串是否都是数字
    static bool isdigit(const string &sInput);
    //字符串转换成时间结构
    static int strTotm(const string &sString, const string &sFormat, struct tm &stTm);
    //时间转换为字符串,默认格式为sFormat格式
    static string tmTostr(const struct tm &stTm, const string &sFormat = "%Y%m%d%H%M%S");
    static string tmTostr(const time_t &t, const string &sFormat = "%Y%m%d%H%M%S");
    //当前时间转换为字符串
    static string nowTostr(const string &sFormat = "%Y%m%d%H%M%S");
    //获取当前时间的毫秒数
    static int64_t nowToms();
    //获取当前时间的微秒数
    static int64_t nowTous();
    //补充区域
};
实现
string YR_Common::trim(const string &sStr, const string &s, bool bChar)
{
    if(sStr.empty())
        return sStr;
    if(!bChar)		//当bChar为false,则去除s字符串
        //先去掉字符串左边的字符串,再去掉右边的字符串
        return trimright(trimleft(sStr, s, false), s, false);
    return trimright(trimleft(sStr, s, true), s, true);
}

string YR_Common::trimleft(const string &sStr, const string &s, bool bChar)
{
    if(sStr.empty())
        return sStr;
	//如果bChar为false,则去除s字符串
    if(!bChar)
    {
        if(sStr.length() < s.length())
            return sStr;
        if(sStr.compare(0, s.length(), s) == 0)
        {
            return sStr.substr(s.length());
        }
        return sStr;
    }
    /**
    * 去掉sStr左边的 字符串s中的字符
    */
    return sStr.substr(sStr.find_first_not_of(s));
}

string YR_Common::trimright(const string &sStr, const string &s, bool bChar)
{
    if(sStr.empty())
        return sStr;
    /**
    * 去掉sStr右边的字符串s
    */
    if(!bChar)
    {
        if(sStr.length() < s.length())
        {
            return sStr;
        }
        if(sStr.compare(sStr.length() - s.length(), s.length(), s) == 0)
        {
            return sStr.substr(0, sStr.length() - s.length());
        }

        return sStr;
    }
    /**
    * 去掉sStr右边的 字符串s中的字符
    */
    return sStr.substr(0, sStr.find_first_not_of(s));
}
//toupper和tolower原型:
//int toupper(int c);
//int tolower(int c);
string YR_Common::lower(const string &s)
{
    string sStr = s;
    for(int i=0;i<sStr.length();i++)
        sStr[i] = tolower(sStr[i]);
    return sStr;
}
string YR_Common::upper(const string &s)
{
    string sStr = s;
    for(int i=0;i<sStr.length();i++)
        sStr[i] = toupper(sStr[i]);
    return sStr;
}

bool YR_Common::isdigit(const string &sInput)
{
    return std::count_if(sInput.begin(),sInput.end(),[](char c){
        return ::isdigit(c);
    }) == static_cast<int32_t>(sInput.length());
}
//strptime和strftime原型:
// #include <time.h>
//char *strptime(const char *s, const char *format, struct tm *tm);
//std::size_t strftime( char* str, std::size_t count, const char* format, const std::tm* time );
//struct tm {
//      int tm_sec;    /* Seconds (0-60) */
//      int tm_min;    /* Minutes (0-59) */
//      int tm_hour;   /* Hours (0-23) */
//      int tm_mday;   /* Day of the month (1-31) */
//      int tm_mon;    /* Month (0-11) */
//      int tm_year;   /* Year - 1900 */
//      int tm_wday;   /* Day of the week (0-6, Sunday = 0) */
//      int tm_yday;   /* Day in the year (0-365, 1 Jan = 0) */
//      int tm_isdst;  /* Daylight saving time */
//};

int YR_Common::strTotm(const string &sString, const string &sFormat, struct tm &stTm)
{
    char *p = strptime(sString.c_str(), sFormat.c_str(), &stTm);
    return (p != NULL) ? 0 : -1;
}
string YR_Common::tmTostr(const struct tm &stTm, const string &sFormat)
{
    char sTimeString[255] = "\0";
    strftime(sTimeString, sizeof(sTimeString), sFormat.c_str(), &stTm);
    return string(sTimeString);
}
string YR_Common::tmTostr(const time_t &t, const string &sFormat)
{
    struct tm tt;
    localtime_r(&t, &tt);
    return tmTostr(tt, sFormat);
}
string YR_Common::nowTostr(const string &sFormat)
{
    time_t t = time(NULL);
    return tmTostr(t, sFormat.c_str());
}
//原型:
// #include <sys/time.h>
//int gettimeofday(struct timeval *tv, struct timezone *tz);
//int settimeofday(const struct timeval *tv, const struct timezone *tz);
//struct timeval {
//	time_t      tv_sec;     /* seconds */
//	suseconds_t tv_usec;    /* microseconds */
//};

int64_t YR_Common::nowToms()
{
    struct timeval tv;
    gettimeofday(&tv, 0);
    return tv.tv_sec * (int64_t)1000 + tv.tv_usec/1000;
}

int64_t YR_Common::nowTous()
{
    struct timeval tv;
    gettimeofday(&tv, 0);
    return tv.tv_sec * (int64_t)1000000 + tv.tv_usec;
}

未完待续~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值