c++ 常用字符串封装函数
Posted qianbo_insist
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c++ 常用字符串封装函数相关的知识,希望对你有一定的参考价值。
常用字符串封装方法
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);
#else
std::size_t nwritten = vsnprintf(buffer, 1023, fmt, args);
#endif
return 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++ 常用字符串封装函数的主要内容,如果未能解决你的问题,请参考以下文章
我的Android进阶之旅NDK开发之在C++代码中使用Android Log打印日志,打印出C++的函数耗时以及代码片段耗时详情