概述

C++在处理字符串时相对于python等脚本语言并没有什么优势,下面将常用的字符串处理函数封装成一个String工具类,方便以后使用,后期还会对该类进行扩充,下面是具体的实现:

// String.hpp
#ifndef _STRING_HPP
#define _STRING_HPP #include <string.h>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector> class String
{
public:
static std::string trimLeft(const std::string& str, const std::string& token = " ")
{
std::string t = str;
t.erase(0, t.find_first_not_of(token));
return t;
} static std::string trimRight(const std::string& str, const std::string& token = " ")
{
std::string t = str;
t.erase(t.find_last_not_of(token) + 1);
return t;
} static std::string trim(const std::string& str, const std::string& token = " ")
{
std::string t = str;
t.erase(0, t.find_first_not_of(token));
t.erase(t.find_last_not_of(token) + 1);
return t;
} static std::string toLower(const std::string& str)
{
std::string t = str;
std::transform(t.begin(), t.end(), t.begin(), tolower);
return t;
} static std::string toUpper(const std::string& str)
{
std::string t = str;
std::transform(t.begin(), t.end(), t.begin(), toupper);
return t;
} static bool startsWith(const std::string& str, const std::string& substr)
{
return str.find(substr) == 0;
} static bool endsWith(const std::string& str, const std::string& substr)
{
return str.rfind(substr) == (str.length() - substr.length());
} static bool equalsIgnoreCase(const std::string& str1, const std::string& str2)
{
return toLower(str1) == toLower(str2);
} static std::vector<std::string> split(const std::string& str, const std::string& delimiter)
{
char* save = nullptr;
char* token = strtok_r(const_cast<char*>(str.c_str()), delimiter.c_str(), &save);
std::vector<std::string> result;
while (token != nullptr)
{
result.emplace_back(token);
token = strtok_r(nullptr, delimiter.c_str(), &save);
}
return result;
}
}; #endif

下面是String的具体使用例子:

// main.cpp
#include <iostream>
#include "String.hpp" int main()
{
std::string str = "Hello world";
std::cout << String::trimLeft(str, "Hello") << std::endl;
std::cout << String::trimRight(str, "world") << std::endl;
str = " nihao ";
std::cout << String::trim(str) << std::endl;
std::cout << String::toUpper(str) << std::endl;
std::cout << String::toLower(String::toUpper(str)) << std::endl;
str = "Hello world";
std::cout << String::startsWith(str, "Hello") << std::endl;
std::cout << String::endsWith(str, "a") << std::endl;
std::vector<std::string> result = String::split(str, " ");
for (auto& iter : result)
{
std::cout << iter << std::endl;
} return 0;
}

该例子的github地址:https://github.com/chxuan/samples/tree/master/String

最新文章

  1. MWeb 1.7.1 版发布!支持导出为 RTF 和 Docx、发布到 Evernote 带样式、文档库备份和新网站主题等大量改进!
  2. SPSS数据分析—多维尺度分析
  3. Linux下Mysql安装
  4. float浮动与清除浮动
  5. linux shell自定义函数(定义、返回值、变量作用域)介绍
  6. 使用uWSGI+nginx部署Django项目
  7. ef左联三张表案例
  8. DLL 导出函数
  9. sublime编辑器代码背景刺眼怎么修改?
  10. FTP配置之 chroot_list 用户切换文件夹
  11. viewport的故事(一)
  12. PDF 补丁丁 0.6.0.3363 版发布(修复无法保存应用程序设置的问题)
  13. Linux 驱动——Button驱动4(fasync)异步通知
  14. linux环境,通过rpm删除mysql包,报错:error reading information on service mysqld: Invalid argument
  15. python分享题目
  16. Thymeleaf 标准表达式语法
  17. Linux中pid_t类型为int类型
  18. [置顶] 滴滴插件化VirtualAPK框架原理解析(二)之Service 管理
  19. XMPP后台搭建
  20. 09.安装Collabora Online服务

热门文章

  1. initWithSpriteFrameName和createWithSpriteFrameName
  2. ajax 加载不同数据
  3. Android将ScrollView移动到最底部
  4. vim之pydiction插件
  5. android 自定义用相机拍照后的照片存储位置
  6. StructLayout特性
  7. 转载 在 Linux 虚拟机中手动安装或升级 VMware Tools
  8. date之Hi时间的思考
  9. ASP.NET MVC- 在Area里使用RedirectToAction跳转出错的解决方法
  10. OC:通讯录实战