SHA256
98 lines
3.4 KiB
C++
98 lines
3.4 KiB
C++
#pragma once
|
|
|
|
#include <string>
|
|
#include <vector>
|
|
#include <algorithm>
|
|
#include <cctype>
|
|
#include <iostream>
|
|
|
|
namespace bstl {
|
|
|
|
class String {
|
|
public:
|
|
String();
|
|
String(std::string str);
|
|
String(const char* str);
|
|
String(const String& other) = default;
|
|
String& operator=(const String& other) = default;
|
|
|
|
// 获取标准字符串
|
|
[[nodiscard]] std::string str() const;
|
|
[[nodiscard]] const char* c_str() const;
|
|
[[nodiscard]] size_t length() const;
|
|
[[nodiscard]] size_t size() const;
|
|
[[nodiscard]] bool empty() const;
|
|
|
|
// 获取/修改字符
|
|
[[nodiscard]] char at(size_t pos) const;
|
|
char operator[](size_t pos) const;
|
|
char& operator[](size_t pos);
|
|
|
|
// 分割 - 按分隔符分割字符串
|
|
[[nodiscard]] std::vector<String> split(const String& delimiter = " ") const;
|
|
|
|
// 连接 - 用分隔符连接多个字符串
|
|
static String join(const std::vector<String>& parts, const String& delimiter = "");
|
|
|
|
// 替换 - 替换第一个/所有匹配项
|
|
[[nodiscard]] String replace(const String& oldStr, const String& newStr, int count = -1) const;
|
|
|
|
// 大小写转换
|
|
[[nodiscard]] String upper() const;
|
|
[[nodiscard]] String lower() const;
|
|
[[nodiscard]] String capitalize() const; // 首字母大写
|
|
[[nodiscard]] String title() const; // 每个单词首字母大写
|
|
|
|
// 判断函数
|
|
[[nodiscard]] bool startswith(const String& prefix) const;
|
|
[[nodiscard]] bool endswith(const String& suffix) const;
|
|
[[nodiscard]] bool contains(const String& substring) const;
|
|
[[nodiscard]] bool isdigit() const;
|
|
[[nodiscard]] bool isalpha() const;
|
|
[[nodiscard]] bool isalnum() const;
|
|
[[nodiscard]] bool isspace() const;
|
|
[[nodiscard]] bool isupper() const;
|
|
[[nodiscard]] bool islower() const;
|
|
|
|
// 查找和计数
|
|
[[nodiscard]] int find(const String& substring, size_t start = 0) const;
|
|
[[nodiscard]] int rfind(const String& substring) const; // 从后往前查找
|
|
[[nodiscard]] int count(const String& substring) const;
|
|
|
|
// 去除空格和其他字符
|
|
[[nodiscard]] String strip() const; // 去除前后空格
|
|
[[nodiscard]] String lstrip() const; // 去除前导空格
|
|
[[nodiscard]] String rstrip() const; // 去除尾部空格
|
|
[[nodiscard]] String strip(const String& chars) const; // 去除指定字符
|
|
|
|
// 对齐和填充
|
|
[[nodiscard]] String ljust(size_t width, char fillchar = ' ') const;
|
|
[[nodiscard]] String rjust(size_t width, char fillchar = ' ') const;
|
|
[[nodiscard]] String center(size_t width, char fillchar = ' ') const;
|
|
|
|
// 字符串转换
|
|
[[nodiscard]] int toInt() const;
|
|
[[nodiscard]] long long toLongLong() const;
|
|
[[nodiscard]] double toDouble() const;
|
|
|
|
// 运算符重载
|
|
String operator+(const String& other) const;
|
|
String operator+(const char* str) const;
|
|
String& operator+=(const String& other);
|
|
bool operator==(const String& other) const;
|
|
bool operator!=(const String& other) const;
|
|
bool operator<(const String& other) const;
|
|
bool operator>(const String& other) const;
|
|
bool operator<=(const String& other) const;
|
|
bool operator>=(const String& other) const;
|
|
|
|
private:
|
|
std::string m_data;
|
|
};
|
|
|
|
// 流运算符重载
|
|
std::ostream& operator<<(std::ostream& os, const String& str);
|
|
std::istream& operator>>(std::istream& is, String& str);
|
|
|
|
} // namespace bstl
|