TpString 类
V0.2.1TpUtils
2025-11-14
介绍
继承自 std::string 的字符串封装类,提供了丰富的扩展操作方法
该类扩展了标准字符串的功能,包括查找、分割、转换、替换等实用方法, 并支持基于逻辑位置(考虑UTF-8字符)的操作。
基础信息
| 头文件: | include <TpString.h> |
|---|---|
| Cmake: | None |
| 发布版本: | V0.2.1 |
| 继承类: | |
| 派生类: |
Public 成员函数
| TpString() | 默认构造函数 | |
|---|---|---|
| TpString(const char &ch) | 通过单个字符构造 | |
| TpString(const char *value) | 通过C风格字符串构造 | |
| TpString(const std::string &value) | 通过 std::string 对象构造 | |
| TpString TpString::operator= | operator=(const std::string &_others) | 赋值运算符重载(std::string) |
| TpString TpString::operator= | operator=(const char *_others) | 赋值运算符重载(C风格字符串) |
| operator==(const TpString &_others) const | 相等运算符重载 | |
| indexOf(const TpString &str, int32_t from=0) const | 查找子字符串的位置 | |
| indexOf(char ch, int32_t from=0) const | 查找字符的位置 | |
| lastIndexOf(const TpString &str, int32_t from=0) const | 从字符串末尾开始查找子字符串的位置 | |
| lastIndexOf(char ch, int32_t from=0) const | 从字符串末尾开始查找字符的位置 | |
| TpList< TpString > TpString::split | split(const char &_ch) const | 指定字符分割字符串 |
| TpString TpString::simplified | simplified() const | 去除首尾空格,中间空格只保留一个 |
| TpString TpString::trimmed | trimmed() const | 去除字符串两端的空白字符 |
| TpString TpString::replace | replace(const TpString &from, const TpString &to) const | 指定字符串替换 |
| TpString TpString::replace | replace(const TpRegex ®ex, const TpString &to) const | 使用正则表达式进行字符串替换 |
| TpString TpString::replace | replace(uint64_t pos, uint64_t len, const TpString &str) | 替换字符串中指定位置的子串 |
| contains(const TpString &find) const | 判断是否包含某个字符串 | |
| TpString TpString::mid | mid(const uint32_t &_pos, const int32_t &_count=-1) const | 截取子字符串 |
| remove(const uint32_t &_pos, const uint32_t &_count=1) | 移除指定位置的字符 | |
| toShort(int32_t base=10) const | 转换为16位有符号整数 | |
| toInt(int32_t base=10) const | 转换为32位有符号整数 | |
| toDouble() const | 转换为双精度浮点数 | |
| toBool() const | 转换为布尔值 | |
| toUShort(bool *ok=nullptr, int base=10) const | 转换为16位无符号整数 | |
| toUInt(bool *ok=nullptr, int base=10) const | 转换为32位无符号整数 | |
| TpString TpString::toUpper | toUpper() const | 转换为大写字符串 |
| TpString TpString::toLower | toLower() const | 转换为小写字符串 |
| TpString TpString::leftJustified | leftJustified(uint32_t width, char fill=' ') const | 在字符串左侧填充字符至指定宽度 |
| TpString TpString::rightJustified | rightJustified(uint32_t width, char fill=' ') const | 在字符串右侧填充字符至指定宽度 |
| TpString TpString::repeated | repeated(uint32_t times) const | 重复字符串指定次数 |
| TpString TpString::toHex | toHex(char separator='\0') const | 获取字符的十六进制表示 |
| containsAnyOf(const TpString &charSet) const | 检查字符串是否包含指定字符集中的任何字符 | |
| containsOnly(const TpString &charSet) const | 检查字符串是否仅包含指定字符集中的字符 | |
| TpString TpString::removeChar | removeChar(char ch) const | 删除所有指定字符 |
| TpString TpString::reversed | reversed() const | 反转字符串 |
| insert(const uint32_t &pos, const TpString &str) | 在指定位置插入字符串 | |
| append(const TpString &str) | 在字符串末尾追加字符串 | |
| TpString TpString::left | left(const uint32_t &_length) | 从左侧截取指定长度字符串 |
| TpString TpString::right | right(const uint32_t &_length) | 从右侧截取指定长度字符串 |
| startsWith(const TpString &prefix) const | 判断字符串是否以指定前缀开头 | |
| endsWith(const TpString &suffix) const | 判断字符串是否以指定后缀结尾 | |
| logicalLength() const | 获取字符串的逻辑长度(考虑UTF-8编码,中英文字符各算一个) | |
| logicalPosToBytePos(uint32_t logicalPos) const | 将逻辑位置转换为字节位置 | |
| nextCharBytePos(uint32_t currentBytePos) const | 获取下一个字符的字节位置 | |
| prevCharBytePos(uint32_t currentBytePos) const | 获取前一个字符的字节位置 | |
| static TpString TpString::number | number(int32_t num, int32_t width=0, char fillChar='0') | 将数字转换为字符串 |
| static TpString TpString::number | number(uint32_t num, int32_t width=0, char fillChar='0') | |
| static TpString TpString::number | number(uint64_t num, int32_t width=0, char fillChar='0') | |
| static TpString TpString::number | number(double num, int32_t precision=2, int32_t width=0, char fillChar='0') |
成员函数说明
TpString::TpString()
默认构造函数...
默认构造函数
TpString::TpString(const char &ch)
通过单个字符构造...
通过单个字符构造
参数:ch: 用于构造的字符
TpString::TpString(const char *value)
通过C风格字符串构造...
通过C风格字符串构造
参数:value: C风格字符串指针
TpString::TpString(const std::string &value)
通过 std::string 对象构造...
通过 stdstring 对象
TpString TpString::operator=(const std::string &_others)
赋值运算符重载(std::string)...
赋值运算符重载(stdstring 对象
返回值:赋值后的 对象
TpString TpString::operator=(const char *_others)
赋值运算符重载(C风格字符串)...
赋值运算符重载(C风格字符串)
参数:_others: 源C风格字符串指针
返回值:赋值后的 对象
bool TpString::operator==(const TpString &_others) const
相等运算符重载...
相等运算符重载
参数:_others: 要比较的 对象
返回值:如果字符串相等返回 true,否则返回 false
int32_t TpString::indexOf(const TpString &str, int32_t from=0) const
查找子字符串的位置...
查找子字符串的位置
参数:str: 要查找的子字符串
参数:from: 开始查找的位置(默认为0)
返回值:子字符串的位置索引,如果未找到则返回-1
int32_t TpString::indexOf(char ch, int32_t from=0) const
查找字符的位置...
查找字符的位置
参数:ch: 要查找的字符
参数:from: 开始查找的位置(默认为0)
返回值:字符的位置索引,如果未找到则返回-1
int32_t TpString::lastIndexOf(const TpString &str, int32_t from=0) const
从字符串末尾开始查找子字符串的位置...
从字符串末尾开始查找子字符串的位置
参数:str: 要查找的子字符串
参数:from: 开始查找的位置(默认为0)
返回值:子字符串的位置索引,如果未找到则返回-1
int32_t TpString::lastIndexOf(char ch, int32_t from=0) const
从字符串末尾开始查找字符的位置...
从字符串末尾开始查找字符的位置
参数:ch: 要查找的字符
参数:from: 开始查找的位置(默认为0)
返回值:字符的位置索引,如果未找到则返回-1
TpList< TpString > TpString::split(const char &_ch) const
指定字符分割字符串...
指定字符分割字符串
参数:_ch: 分隔符
返回值:返回分割后的字符串列表
TpString TpString::simplified() const
去除首尾空格,中间空格只保留一个...
去除首尾空格,中间空格只保留一个
返回值:返回处理后的字符串
TpString TpString::trimmed() const
去除字符串两端的空白字符...
去除字符串两端的空白字符
返回值:返回处理后的字符串
TpString TpString::replace(const TpString &from, const TpString &to) const
指定字符串替换...
指定字符串替换
参数:from: 要被替换的字符串
参数:to: 替换后的字符串
返回值:返回替换后的结果
TpString TpString::replace(const TpRegex ®ex, const TpString &to) const
使用正则表达式进行字符串替换...
使用正则表达式进行字符串替换
参数:regex: 正则对象
参数:to: 匹配的字符串替换对象
返回值:返回替换后的结果
TpString TpString::replace(uint64_t pos, uint64_t len, const TpString &str)
替换字符串中指定位置的子串...
替换字符串中指定位置的子串
参数:pos: 起始位置
参数:len: 要替换的长度
参数:str: 替换字符串
返回值:替换后的字符串
bool TpString::contains(const TpString &find) const
判断是否包含某个字符串...
判断是否包含某个字符串
参数:find: 查询字符串
返回值:当前字符串包含 find 返回 true,否则返回 false
TpString TpString::mid(const uint32_t &_pos, const int32_t &_count=-1) const
截取子字符串...
截取子字符串
参数:_pos: 起始索引(0开始)
参数:_count: 截取长度,-1 表示到字符串末尾
返回值:截取后的字符串
void TpString::remove(const uint32_t &_pos, const uint32_t &_count=1)
移除指定位置的字符...
移除指定位置的字符
参数:_pos: 起始索引
参数:_count: 要移除的字符数量
int16_t TpString::toShort(int32_t base=10) const
转换为16位有符号整数...
转换为16位有符号整数
参数:base: 进制(2/8/10/16,默认为10)
返回值:转换结果
int32_t TpString::toInt(int32_t base=10) const
转换为32位有符号整数...
转换为32位有符号整数
参数:base: 进制(2/8/10/16,默认为10)
返回值:转换结果
double TpString::toDouble() const
转换为双精度浮点数...
转换为双精度浮点数
返回值:转换结果
bool TpString::toBool() const
转换为布尔值...
转换为布尔值
返回值:转换结果("true"、"1"等返回true,其余返回false)
uint16_t TpString::toUShort(bool *ok=nullptr, int base=10) const
转换为16位无符号整数...
转换为16位无符号整数
参数:ok: 可选,指示转换是否成功
参数:base: 进制(2/8/10/16,默认为10)
返回值:转换结果
uint32_t TpString::toUInt(bool *ok=nullptr, int base=10) const
转换为32位无符号整数...
转换为32位无符号整数
参数:ok: 可选,指示转换是否成功
参数:base: 进制(2/8/10/16,默认为10)
返回值:转换结果
TpString TpString::toUpper() const
转换为大写字符串...
转换为大写字符串
返回值:大写字符串
TpString TpString::toLower() const
转换为小写字符串...
转换为小写字符串
返回值:小写字符串
TpString TpString::leftJustified(uint32_t width, char fill=' ') const
在字符串左侧填充字符至指定宽度...
在字符串左侧填充字符至指定宽度
参数:width: 总宽度
参数:fill: 填充字符(默认为空格)
返回值:处理后的字符串
TpString TpString::rightJustified(uint32_t width, char fill=' ') const
在字符串右侧填充字符至指定宽度...
在字符串右侧填充字符至指定宽度
参数:width: 总宽度
参数:fill: 填充字符(默认为空格)
返回值:处理后的字符串
TpString TpString::repeated(uint32_t times) const
重复字符串指定次数...
重复字符串指定次数
参数:times: 重复次数
返回值:重复后的字符串
TpString TpString::toHex(char separator='\0') const
获取字符的十六进制表示...
获取字符的十六进制表示
参数:separator: 分隔符(默认为无)
返回值:十六进制表示字符串
bool TpString::containsAnyOf(const TpString &charSet) const
检查字符串是否包含指定字符集中的任何字符...
检查字符串是否包含指定字符集中的任何字符
参数:charSet: 字符集
返回值:如果包含任何字符集中字符返回 true,否则返回 false
bool TpString::containsOnly(const TpString &charSet) const
检查字符串是否仅包含指定字符集中的字符...
检查字符串是否仅包含指定字符集中的字符
参数:charSet: 字符集
返回值:如果仅包含字符集中字符返回 true,否则返回 false
TpString TpString::removeChar(char ch) const
删除所有指定字符...
删除所有指定字符
参数:ch: 要删除的字符
返回值:处理后的字符串
TpString TpString::reversed() const
反转字符串...
反转字符串
返回值:反转后的字符串
void TpString::insert(const uint32_t &pos, const TpString &str)
在指定位置插入字符串...
在指定位置插入字符串
参数:pos: 插入位置索引
参数:str: 要插入的字符串
void TpString::append(const TpString &str)
在字符串末尾追加字符串...
在字符串末尾追加字符串
参数:str: 要追加的字符串
TpString TpString::left(const uint32_t &_length)
从左侧截取指定长度字符串...
从左侧截取指定长度字符串
参数:_length: 截取长度
返回值:截取后的字符串
TpString TpString::right(const uint32_t &_length)
从右侧截取指定长度字符串...
从右侧截取指定长度字符串
参数:_length: 截取长度
返回值:截取后的字符串
bool TpString::startsWith(const TpString &prefix) const
判断字符串是否以指定前缀开头...
判断字符串是否以指定前缀开头
参数:prefix: 前缀字符串
返回值:如果以指定前缀开头返回 true,否则返回 false
bool TpString::endsWith(const TpString &suffix) const
判断字符串是否以指定后缀结尾...
判断字符串是否以指定后缀结尾
参数:suffix: 后缀字符串
返回值:如果以指定后缀结尾返回 true,否则返回 false
uint32_t TpString::logicalLength() const
获取字符串的逻辑长度(考虑UTF-8编码,中英文字符各算一个...
获取字符串的逻辑长度(考虑UTF-8编码,中英文字符各算一个)
返回值:逻辑长度
uint32_t TpString::logicalPosToBytePos(uint32_t logicalPos) const
将逻辑位置转换为字节位置...
将逻辑位置转换为字节位置
参数:logicalPos: 逻辑位置
返回值:对应的字节位置
uint32_t TpString::nextCharBytePos(uint32_t currentBytePos) const
获取下一个字符的字节位置...
获取下一个字符的字节位置
参数:currentBytePos: 当前字节位置
返回值:下一个字符的字节位置
uint32_t TpString::prevCharBytePos(uint32_t currentBytePos) const
获取前一个字符的字节位置...
获取前一个字符的字节位置
参数:currentBytePos: 当前字节位置
返回值:前一个字符的字节位置
static TpString TpString::number(int32_t num, int32_t width=0, char fillChar='0')
将数字转换为字符串...
将数字转换为字符串
参数:num: 数字
返回值:返回转换后的字符串
