ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

C++字符串string的常用方法

2026/8/5 13:26:58 拓冰建站 浏览量
C++字符串string的常用方法 一、基础构造与初始化#include string using namespace std; int main() { // 1. 默认构造空字符串 string s1; // 2. 用C风格字符串char*初始化 string s2(hello); string s3 world; // 等价于上面 // 3. 拷贝构造用另一个string初始化 string s4(s2); // s4 hello string s5 s2; // 等价 // 4. 用部分字符初始化 string s6(hello, 3); // 取前3个字符 → hel string s7(s2, 1, 3); // 从s2的索引1开始取3个 → ell // 5. 用重复字符初始化 string s8(5, a); // 5个a → aaaaa // 6. C11初始化列表 string s9{h, e, l, l, o}; return 0; }二、赋值与修改string s; // 1. 直接赋值 s hello; s string(world); s a; // 单个字符也可以 // 2. assign()更灵活的赋值 s.assign(hello); // 直接赋值 s.assign(hello, 3); // 取前3个 → hel s.assign(5, x); // 5个x → xxxxx s.assign(s2, 1, 2); // 从s2索引1取2个 // 3. swap()交换两个字符串的内容 string a apple, b banana; a.swap(b); // 现在abanana, bapple三、访问字符索引 / 迭代器string s hello; // 1. [] 下标访问不检查越界越界会崩溃 char c1 s[0]; // h s[0] H; // 修改为 Hello // 2. at()带越界检查的访问越界会抛异常更安全 char c2 s.at(1); // e // 3. 首尾字符 char first s.front(); // C11等价 s[0] char last s.back(); // C11等价 s[s.size()-1] // 4. 迭代器遍历类似指针 for (auto it s.begin(); it ! s.end(); it) { cout *it; // 依次输出 h e l l o } // 反向迭代器 for (auto it s.rbegin(); it ! s.rend(); it) { cout *it; // 依次输出 o l l e h }四、字符串拼接最常用string s1 hello, s2 world; // 1. 运算符最直观 string s3 s1 s2; // hello world s1 !; // s1 变成 hello! // 2. append()更灵活的拼接 s1.append(s2); // 直接拼s2 s1.append(abc, 2); // 拼abc的前2个 → ab s1.append(3, x); // 拼3个x → xxx s1.append(s2, 1, 2); // 拼s2从索引1开始的2个字符 // 3. push_back()只拼单个字符 s1.push_back(!);五、字符串比较string a apple, b banana; // 1. 直接用比较运算符按字典序/ASCII码比较 if (a b) cout 相等; if (a ! b) cout 不等; if (a b) cout a字典序在b前面; // apple banana 为真 if (a b) cout a字典序在b后面; if (a b) cout 小于等于; if (a b) cout 大于等于; // 2. compare()更详细的比较返回int // 返回0相等返回0a b返回0a b int res a.compare(b); res a.compare(1, 3, b); // a从索引1开始的3个字符 和 b 比较 res a.compare(1, 3, b, 0, 2); // a的[1,3) 和 b的[0,2) 比较六、查找与搜索解析 HTTP/URL 必备这是你处理网络请求最常用的操作比如解析 URI、找参数位置。string s hello world hello; // 1. find()从前往后找返回第一次出现的索引找不到返回 string::npos size_t pos s.find(hello); // 返回 0 pos s.find(hello, 1); // 从索引1开始找返回 12 pos s.find(w); // 找单个字符返回 6 pos s.find(abc); // 找不到返回 string::npos // 2. rfind()从后往前找反向查找 pos s.rfind(hello); // 从后往前找返回 12 // 3. find_first_of()找任意一个匹配的字符 pos s.find_first_of(aeiou); // 找第一个元音字母返回 1e // 4. find_last_of()找最后一个匹配的字符 pos s.find_last_of(aeiou); // 返回 13最后一个l前面的o // 5. find_first_not_of()找第一个不匹配的字符 pos s.find_first_not_of(helo);// 找第一个不是h/e/l/o的字符返回5空格 // 【常用判断】找不到的处理 if (pos string::npos) { cout 没找到; }七、截取、替换、插入、删除1. 截取substr解析 URI / 参数必备string s hello world; // substr(pos, len)从pos开始截取len个字符 string sub1 s.substr(0, 5); // 从0开始取5个 → hello string sub2 s.substr(6); // 从6开始取到末尾 → world string sub3 s.substr(6, 3); // 从6开始取3个 → wor2. 替换replacestring s hello world; // replace(pos, len, new_str)从pos开始替换len个字符为new_str s.replace(6, 5, C); // 把world换成C → hello C // 更灵活的替换 s.replace(0, 5, hi, 2); // 替换前5个为hi的前2个3. 插入insertstring s hello; // insert(pos, str)在pos位置插入str s.insert(5, world); // 在索引5插入 → hello world s.insert(0, 2, x); // 在开头插入2个x → xxhello world4. 删除erasestring s hello world; // erase(pos, len)从pos开始删除len个字符 s.erase(5, 6); // 删除从5开始的6个 → hello s.erase(0); // 从0删到末尾 → 空字符串 s.clear(); // 清空字符串等价 s.erase() // 用迭代器删除 s.erase(s.begin()); // 删除第一个字符 s.erase(s.begin(), s.begin()3); // 删除前3个字符八、字符串转换与数字互转、C 风格字符串互转1. 与 C 风格字符串char*互转string s hello; // string → const char*只读不要修改返回值 const char* c_str s.c_str(); const char* data s.data(); // C11前和c_str略有不同C11后等价 // char* → string直接赋值或构造即可 char arr[] world; string s2(arr);2. 与数字互转C11 标准库函数最方便// 【数字 → string】 string s; s to_string(123); // int → 123 s to_string(45.67); // double → 45.670000 s to_string(true); // bool → 1 // 【string → 数字】 string num_str 123; int i stoi(num_str); // string → int long l stol(num_str); // string → long long long ll stoll(num_str); // string → long long float f stof(3.14); // string → float double d stod(3.14159); // string → double // 进阶带进制和位置参数 size_t pos; int hex stoi(0x1a, pos, 16); // 16进制转10进制 → 26九、其他常用操作string s hello; // 1. 大小/长度 if (s.empty()) cout 空字符串; int len s.size(); // 等价 s.length()推荐用size()和STL容器统一 // 2. 调整大小 s.resize(10, x); // 把s调整为10个字符多出来的用x填充 → helloxxxxx s.resize(3); // 截断为3个字符 → hel // 3. 预留空间优化性能避免频繁扩容 s.reserve(100); // 预留100个字符的空间但不改变size() int cap s.capacity(); // 获取当前容量 // 4. C17string_view只读视图零拷贝性能优化 // 适合只需要读取、不需要修改字符串的场景避免拷贝开销 string_view sv s; // 直接指向s的内存不拷贝 cout sv[0]; // 读取和string一样十、给你的网络编程场景实用小技巧1. 解析 HTTP 请求行GET /index.html HTTP/1.1string req_line GET /index.html HTTP/1.1; size_t space1 req_line.find( ); size_t space2 req_line.find( , space1 1); string method req_line.substr(0, space1); // GET string uri req_line.substr(space1 1, space2 - space1 - 1); // /index.html string version req_line.substr(space2 1); // HTTP/1.1//更好的方法 std::string req_line GET /index.html HTTP/1.1; std::string method, uri, version; std::istringstream iss(req_line); iss method uri version;2. 去除字符串首尾空格string trim(string s) { // 去除开头空格 s.erase(0, s.find_first_not_of( \t\n\r)); // 去除末尾空格 s.erase(s.find_last_not_of( \t\n\r) 1); return s; }3. 分割字符串按分隔符拆分vectorstring split(const string s, char delimiter) { vectorstring tokens; string token; istringstream tokenStream(s); while (getline(tokenStream, token, delimiter)) { tokens.push_back(token); } return tokens; } // 使用split(a,b,c, ,) → 返回 {a,b,c}