推荐一个更好用的C++字符串处理工具类TpString,解决中文字符长度计算等问题!
TpString 使用指南:更好用的C++字符串处理类
引言
在项目开发中,TinyPiXOS 平台提供了 TpString 类作为统一的字符串处理方案 。本文将介绍 TpString 的核心使用方法,特别是相比 std::string 的独特功能。
核心使用方法
1. 基础操作
创建和初始化
// 从 C 字符串创建
TpString str1 = "Hello TinyPiX";// 从 std::string 转换
std::string stdStr = "World";
TpString str2 = stdStr.c_str();// 空字符串
TpString emptyStr;
获取 C 风格字符串
TpString text = "示例文本";
const char* cStr = text.c_str();
长度和空判断
TpString text = "Hello";
int len = text.length(); // 获取长度
bool isEmpty = text.empty(); // 判断是否为空
中文字符长度计算 ⭐
TpString 独有功能: logicalLength() 方法可以正确计算包含中文字符的字符串长度,这比 std::string::length() 更适合处理多字节字符。
TpString text = "Hello世界"; // 字节长度(包含多字节字符的实际字节数)
int byteLen = text.length(); // 返回 11 (5 + 6) // 逻辑长度(字符数量)
int charLen = text.logicalLength(); // 返回 7 (5 + 2)
2. 字符串比较
TpString str1 = "svg";
TpString str2 = "png";// 比较字符串,返回 0 表示相等
if (str1.compare("svg") == 0) {// 字符串相等
}
3. 字符串替换 ⭐ (支持链式调用)
TpString 独有优势: replace() 方法支持链式调用,可以连续进行多次替换操作,这比 std::string 更简洁。
TpString format = "yyyy-MM-dd HH:mm:ss";// 链式调用进行多次替换
TpString converted = format.replace("yyyy", "%Y").replace("MM", "%m").replace("dd", "%d").replace("HH", "%H").replace("mm", "%M").replace("ss", "%S");
// 结果: "%Y-%m-%d %H:%M:%S"
在时间格式化中也有类似应用 。
4. 字符串查找
TpString text = "Hello World";// 查找子串位置
size_t pos = text.find("World");
if (pos != TpString::npos) {// 找到子串
}
5. 字符串包含检查 ⭐
TpString 独有功能: contains() 方法直接检查是否包含子串,比 std::string 的 find() != npos 更直观。
TpString colorValue = "linear-gradient(...)";// 直接检查是否包含子串
if (colorValue.contains("gradient")) {// 包含渐变
}
6. 字符串简化 ⭐
TpString 独有功能: simplified() 方法可以去除字符串首尾空白并压缩内部连续空白为单个空格。
TpString cssValue = " 100px ";
TpString simplified = cssValue.simplified();
// 结果: "100px"
7. 字符串分割 ⭐
TpString 独有功能: split() 方法可以按分隔符分割字符串,返回字符串数组,比 std::string 需要手动实现更方便。
TpString dateTime = "2024-01-15 10:30:00";// 按空格分割
auto parts = dateTime.split(' ');
// parts[0] = "2024-01-15"
// parts[1] = "10:30:00"
8. 字符串大小调整
TpString buffer;
buffer.resize(1024); // 预分配 1024 字节// 读取数据后调整实际大小
buffer.resize(actualSize);
9. 获取字符串大小
TpString data = "Hello";
size_t size = data.size(); // 返回字符串长度
10. 字符访问和修改
TpString str = "Hello";// 通过下标访问字符
char firstChar = str[0]; // 'H'// 修改字符
str[0] = 'h'; // "hello"
11. 字符串拼接
TpString path = "/";
TpString part1 = "usr";
TpString part2 = "local";// 使用 += 拼接
path += part1 + "/";
path += part2 + "/";
// 结果: "/usr/local/"
12. 移除末尾字符 ⭐
TpString 独有功能: pop_back() 方法可以移除字符串末尾的字符。
TpString path = "/usr/local/";
path.pop_back(); // 移除末尾的 "/"
// 结果: "/usr/local"
13. 子串提取
TpString format = "yyyy-MM-dd HH:mm:ss";// 提取日期部分
TpString datePart = format.substr(0, format.find(' '));
// 结果: "yyyy-MM-dd"// 提取时间部分
TpString timePart = format.substr(format.find(' ') + 1);
// 结果: "HH:mm:ss"
TpString 独有功能总结
相比 std::string,TpString 提供了以下独特功能:
- 中文字符长度计算
logicalLength() - 链式调用的
replace(): 支持连续替换操作,让代码更简洁 contains()方法: 直接检查子串包含,无需find() != npossimplified()方法: 自动去除首尾空白并压缩内部空白split()方法: 按分隔符分割字符串,返回数组pop_back()方法: 移除末尾字符- 与平台深度集成: 与 TinyPiXOS 各组件无缝配合
TinyPiXOS开发者联盟
源码级支持 + 真实项目:TinyPiXOS开发者联盟招募中。
获取开发资料
技术手册
项目官网
B 站视频
感谢支持和关注,如果对项目感兴趣,请点赞、收藏和转发!
