C++ 编程之std::string的utils
2018-01-03 16:57
459 查看
众所周知, C++标准库的std::string 如果用来处理字符串的话, 会很不友好,譬如使用字符串拆分split, 字符串数组组合啊, 字符串大小写转换之类的
本文讲述的是一些通过标准库现有接口实现的部分字符串操作方法
我这里写了两个版本, 从名字看, 第一种适合拆分分割的字符串出现次数比较多的情况, 第二种比较适合拆分分割字符串出现次数相当少或者没有的情况
本文讲述的是一些通过标准库现有接口实现的部分字符串操作方法
1. split
#include <vector> int count(const std::string& string, const std::string& sep) { int cnt = 0; std::string::size_type index = string.find(sep, 0); while(index != std::string::npos) { ++cnt; index = string.find(sep, index + sep.size()); } return cnt; } std::vector<std::string> split(const std::string& string, const std::string& sep) { std::vector<std::string> result; result.reserve(count(string, sep) + 1); std::string::size_type start = 0; std::string::size_type index = string.find(sep, start); while(index != std::string::npos) { result.push_back(string.substr(start, index - start)); start = index + sep.size(); index = string.find(sep, start); } result.push_back(string.substr(start)); return result; } std::vector<std::string> split_with_little_sep(const std::string& string, const std::string& sep) { std::vector<std::string> result; std::string::size_type start = 0; std::string::size_type index = string.find(sep, start); while(index != std::string::npos) { result.push_back(string.substr(start, index - start)); start = index + sep.size(); index = string.find(sep, start); } result.push_back(string.substr(start)); return result; }
我这里写了两个版本, 从名字看, 第一种适合拆分分割的字符串出现次数比较多的情况, 第二种比较适合拆分分割字符串出现次数相当少或者没有的情况
2. join
#include <sstream> std::string join(const std::string& s, const std::vector<std::string>& v) { if(v.size() == 0) { return std::string(); } auto size = v.size() - 1; std::ostringstream os; for(auto i = 0u; i < size; ++i) { os << v[i] << s; } os << v[size]; return os.str(); }
3. upperCase/lowerCase
#include <string> #include <algorithm> std::string lowerCase(const std::string& s) { std::string result(s); std::transform(s.begin(), s.end(), result.begin(), [](unsigned char c) -> unsigned char { return std::tolower (c); } ); return result; } std::string upperCase(const std::string& s) { std::string result(s); std::transform(s.begin(), s.end(), result.begin(), [](unsigned char c) -> unsigned char { return std::toupper (c); } ); return result; }
4. replace
// source: 原字符串也是结果字符串 // olds: 要替换的部分 // news: 替换后的内容 // times: 替换次数, -1 表示全部替换 void replace_self(std::string& source, const std::string& olds, const std::string& news, int times = -1) { std::string::size_type index = source.find(olds); while(index != std::string::npos && times) { source.replace(index, olds.size(), news); index = source.find(olds); times = times > 0 ? times - 1 : times; } } std::string replace(const std::string& source, const std::string& olds, const std::string& news int times = -1) { std::string dest(source); replace_self(dest, olds, news, times); return dest; }
5. startsWith/endsWith
bool startsWith(const std::string& source, const std::string& head) { return head.size() <= source.size() && source.compare(0, head.size(), head) == 0; } bool endsWith(const std::string& source, const std::string& tail) { return tail.size() <= source.size() && source.compare(source.size() - tail.size(), tail.size(), tail) == 0; }
后续待更新……
相关文章推荐
- How to convert std::string to LPCWSTR in C++ (Unicode)
- C++ std::string --- 你可能不知道的一些用法
- C++: std::string 与 Unicode 如何结合?
- 实战c++中的string系列--std::string与MFC中CString的转换
- C++ std::string --- 你可能不知道的一些用法
- memmove(),memcopy(),memset()对std::string的影响,以及它们在c/c++中正确的使用方法。
- C++ 在DLL中使用std::string乱码的问题
- C++学习3--编程基础(vector、string、三种传参)
- C++ std::string 不可初始化为NULL及基本用法
- C++基础编程----2.2标准库类型string
- [C++]使用std::stringstream小心内存,适时清空缓冲。
- C++:float 转型到 std::string
- 如何在C++中方便的将float、int等类型数据转换成string类型,并利用ROS中的std_msg/String发布出去
- VS2005:C++ std::string, std::wstring转换方法
- C++中如何去掉std::string对象的首尾空格
- C++ 11多线程编程--std::lock_guard类
- C++ STL : std::string
- C++ 的std::string的“读时也拷贝”技术!
- C++中,如何在标准库的std::string和常用库(Qt,VC等)的QString之间进行选择?
- C++ Primer Plus(第五版)第五章编程练习题6(关于string难点哦)