常用字符串封装方法

1 GetAppExePath

得到执行路径

2 str_2

转并且得到字符串

3 int_2

转得到整形

4 isNumber

是否时数字

5 format

字符串格式化

6 split

字符串分割

7 string_to_upper 8 string_to_lower

字符串变大小写

9 ifnote

是否时注释

10 trim

去空格

show me the code

#ifndef  _UTIL_TOOL_H_
#define  _UTIL_TOOL_H_
#define _CRT_SECURE_NO_WARNINGS
#include <string>
#include <direct.h>
#include <vector>
#include <algorithm>
#include <sstream>
#include <stdarg.h>
#include <iostream>
#include <fstream>
//#include <cstdlib>
#ifndef _WIN32
#include <unistd.h>
#endif
using namespace std;
#define MAX_PATH 260
static void GetAppExePath(string &name)
{char path_buffer[MAX_PATH] = { 0 };_getcwd(path_buffer, MAX_PATH);strcat(path_buffer, "/");strcat(path_buffer, name.c_str());name = path_buffer;
}template<typename T>
std::string str_2(const T& t)
{std::ostringstream oss;oss << t;return oss.str();
}template<typename T>
T int_2(const std::string& s)
{std::istringstream iss(s);T x;if (!(iss >> x))return 0;return x;
}static bool isNumber(const std::string& str)
{for (std::size_t i = 0; i < str.length(); i++) {if (!::isdigit(str[i]))return false;}return true;
}static std::string string_vprintf(const char* fmt, va_list args)
{char buffer[1024];
#if defined(_WIN32)std::size_t nwritten = _vsnprintf(buffer, 1023, fmt, args);
#elsestd::size_t nwritten = vsnprintf(buffer, 1023, fmt, args);
#endifreturn string(buffer);
}static std::string format(const char* fmt, ...)
{va_list args;va_start(args, fmt);std::string ret = string_vprintf(fmt, args);va_end(args);return ret;
}static void split(const std::string& s, std::string& delim, std::vector< std::string > &ret)
{size_t last = 0;size_t index = s.find_first_of(delim, last);while (index != std::string::npos){string tt = s.substr(last, index - last);ret.push_back(tt);last = index + 1;index = s.find_first_of(delim, last);}if (index - last>0){ret.push_back(s.substr(last, index - last));}
}static inline void CharUpper(char& c)
{if (c <= 122 && c >= 97)c = c - 32;
}
static inline void CharLower(char& c)
{if (c <= 90 && c >= 65)c = c + 32;
}static inline void string_to_upper(string& strsource)
{void(*FUNC)(char& c);FUNC = CharUpper;for_each(strsource.begin(), strsource.end(), FUNC);
}
static inline void string_to_lower(string& strsource)
{void(*FUNC)(char& c);FUNC = CharLower;for_each(strsource.begin(), strsource.end(), FUNC);
}static bool load_conf_file(const char *filename)
{//std::ifstream myfile("./config.txt");if (filename == NULL)filename = "config.txt";std::ifstream myfile(filename);std::string temp;// , node, nodename;if (!myfile.is_open()){std::cout << "can not open" << std::endl;return false;}cout << "load config file-->" << filename << endl;while (getline(myfile, temp)){}myfile.close();return 0;
}static bool ifnote(const std::string & tmp)
{if (tmp.empty())return false;int l = (int)tmp.length();char a = tmp[0];char b;if (l >= 2)b = tmp[1];if (l == 1)return a == '#';if (l >= 2)return a == '#' || (a == '/'&& b == '/');return false;
}static void trim(std::string &s)
{if (s.empty())return;s.erase(s.find_last_not_of('\r') + 1); //the linux system getline must remove the '\r's.erase(s.find_last_not_of(' ') + 1);//s.erase(s.find_last_not_of('\t') + 1);s.erase(0, s.find_first_not_of(' '));//s.erase(0, s.find_first_not_of('\t'));}
#endif

c++ 常用字符串封装函数相关推荐

  1. C语言常用字符串操作函数大全详解(strstr,strtok,strrchr,strcat,strcmp,strcpy,strerror,strspn,strchr等)

    参考:string.h中常用字符串操作函数说明(strstr,strtok,strrchr,strcat,strcmp,strcpy,strerror,strspn,strchr等) 作者:一只青木呀 ...

  2. python常用字符串处理函数_Python第10课:常用的字符串处理函数

    Python第10课:常用的字符串处理函数 时间 2019-01-17上午10:00 主讲 刘培富 地点 四楼电教室 1.字符的ascii码及其逆运算 ord("x") 求asci ...

  3. 【PHP基础知识】——常用字符串处理函数总结

    一.概要 我们知道,字符串操作是主流web编程语言的基础,也是在日常开发中不可或缺的一项.PHP处理字符串的能力非常强大,方法也是多种多样.文章列举了一些PHP中常见的字符串处理方法. 二.常用字符串 ...

  4. 常用字符串处理函数汇总

    *************************************************** 更多精彩,欢迎进入:http://shop115376623.taobao.com ****** ...

  5. c语言常用字符串处理函数6,【总结】C语言中常见的字符串处理函数

    ------------------------------------------------------------------------------------------- C语言中没有字符 ...

  6. C语言的常用字符串操作函数(一)

    一直做的是单片机相关的程序设计,所以程序设计上更偏向底层,对于字符串的操作也仅限于液晶屏幕上的显示等工作,想提高下字符串操作的水平,而不是笨拙的数组替换等方式,翻看帖子发现C语言的字符串操作函数竟然这 ...

  7. Delphi中常用字符串处理函数

    1.copy(str,pos,num) 从str字符串的pos处开始,截取num个字符的串返回. 假设str为'abcdef',copy(str,3,2)='cd',copy(str,4,10)='d ...

  8. C语言中常用字符串处理函数(总结大全)

    目录 字符串处理函数 1.char *gets(char *s); 2. char *fgets(char *s, intsize, FILE *stream); 3. int puts(const ...

  9. PHP_常用字符串处理函数

    2019独角兽企业重金招聘Python工程师标准>>> addcslashes - 为字符串里面的部分字符添加反斜线转义字符 addslashes - 用指定的方式对字符串里面的字符 ...

最新文章

  1. Switch入门第一讲
  2. 常用的JVM参数,你现在就记好!
  3. include的两种形式、CPP的搜索路径
  4. Tomcat的安装与配置(新手向)
  5. 【Fanvas技术解密】HTML5 canvas实现脏区重绘
  6. Linux系统管理第六周作业【Linux微职位】
  7. web面试 new操作符到底干了什么?
  8. MongoDB数据库基础教程
  9. Javascript 高级程序设计第三版理解
  10. java ssh框架搭建_SSH框架基础搭建
  11. 如何用U盘重新安装Win10系统
  12. 白巧脆皮奶香雪糕,一次成功
  13. 货拉拉数据治理平台建设实践
  14. 思考型人格分析,思考型人格的职业发展方向
  15. android手机系统对比,国产手机系统哪家强?几大主流手机系统盘点对比
  16. (1)1999~2021 年中国城市统计年鉴面板数据(含地级市面板、县级市面板和主要指标)(2)香港统计年刊(3)第一到第七次的人口普查数据(4)全国省市县-五六七普人口数据(5)国际统计年鉴
  17. 2009年A股各板块龙头股大全(转载)
  18. 给2016末尾的阳光一次含蓄问暖
  19. 一个诗人的一生——诗人小G的人生
  20. 手掌静脉识别——利用深度学习进行ROI的选取

热门文章

  1. python做的数据图表怎么在flask中显示_Python:如何在Flask应用程序的表中显示MySQL查询的数据...
  2. 腾讯音乐计划以介绍形式在港交所主板二次上市
  3. 欧拉好猫车主公开信:宣传部对员工学历没有要求么?
  4. 华为P50真机谍照曝光:璀璨粉色机身 牢牢锁定女性用户
  5. 三年后见!雷军透露年轻人的第一台汽车售价......
  6. 英雄联盟更新防沉迷规则:未成年用户节假日每日限玩3小时
  7. 罗永浩与银联合作直播,但因过程太流畅被网友调侃是录播
  8. 网易有道词典2019年度十大热词:Vlog、PUA等上榜
  9. 加速包可能没用!12306屏蔽多个抢票软件
  10. 企业微信3.0版本发布:客户朋友圈功能正式上线