Files
2026-08-16 16:28:12 +08:00

98 lines
2.9 KiB
C++

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