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