string基本概念
本质:
-
string是C++风格的字符串,而string本质上是一个类。
*string和char 区别:
-
char *是一个指针。
-
string是一个类,类内部封装了char*,管理这个字符串,是一个char*型的容器。
特点:
-
string类内部封装了很多成员方法,例如:查找find,拷贝copy,删除delete替换replace,插入insert。
-
string管理char*所分配的内存,不用担心复制越界和取值越界等,由类内部进行负责。
string构造函数
构造函数原型:
-
string(); //创建一个空的字符串 例如: string str;
-
string(const char* s); //使用字符串s初始化
#include <iostream>
#include <string>
int main() {
// 使用 C 风格字符串初始化
const char* cstr = "Hello, World!";
std::string str(cstr);
std::cout << "字符串内容: " << str << std::endl; // 输出 "Hello, World!"
return 0;
}
-
string(const string& str); //使用一个string对象初始化另一个string对象
#include <iostream>
#include <string>
int main() {
// 使用一个 string 对象初始化另一个 string 对象
std::string original = "Hello, World!";
std::string copy(original);
std::cout << "原始字符串: " << original << std::endl; // 输出 "Hello, World!"
std::cout << "复制的字符串: " << copy << std::endl; // 输出 "Hello, World!"
return 0;
}
-
string(int n, char c); //使用n个字符c初始化
#include <iostream>
#include <string>
int main() {
// 使用一个 string 对象初始化另一个 string 对象
std::string original = "Hello, World!";
std::string copy(original);
std::cout << "原始字符串: " << original << std::endl; // 输出 "Hello, World!"
std::cout << "复制的字符串: " << copy << std::endl; // 输出 "Hello, World!"
return 0;
}
string赋值操作
功能描述:
-
给string字符串进行赋值
赋值的函数原型:
-
string& operator=(const char* s); //char*类型字符串 赋给当前的字符串
#include <iostream>
#include <string>
int main() {
// 创建一个空字符串
std::string str;
std::cout << "初始字符串: " << str << std::endl; // 输出空字符串
// 使用 C 风格字符串赋值
const char* cstr = "Hello, World!";
str = cstr; // 使用 operator=(const char* s)
std::cout << "赋值后的字符串: " << str << std::endl; // 输出 "Hello, World!"
return 0;
}
-
string& operator=(const string &s); //把字符串s赋给当前的字符串
#include <iostream>
#include <string>
int main() {
// 创建两个字符串对象
std::string original = "Hello, World!";
std::string target;
std::cout << "原始字符串: " << original << std::endl; // 输出 "Hello, World!"
std::cout << "目标字符串(初始): " << target << std::endl; // 输出空字符串
// 使用 operator=(const string &s) 将 original 赋值给 target
target = original;
std::cout << "目标字符串(赋值后): " << target << std::endl; // 输出 "Hello, World!"
return 0;
}
-
string& operator=(char c); //字符赋值给当前的字符串
#include <iostream>
#include <string>
int main() {
// 创建一个空字符串
std::string str;
std::cout << "初始字符串: " << str << std::endl; // 输出空字符串
// 使用单个字符赋值
char c = 'A';
str = c; // 使用 operator=(char c)
std::cout << "赋值后的字符串: " << str << std::endl; // 输出 "A"
return 0;
}
以下是string中的assign函数,由于assign使用较少且大部分功能可以通过string初始化或运算符进行替代,我们不再进行举例。
-
string& assign(const char *s); //把字符串s赋给当前的字符串
-
string& assign(const char *s, int n); //把字符串s的前n个字符赋给当前的字符串
-
string& assign(const string &s); //把字符串s赋给当前字符串
-
string& assign(int n, char c); //用n个字符c赋给当前字符串
string字符串拼接
功能描述:
-
实现在字符串末尾拼接字符串
函数原型:
-
string& operator+=(const char* str); //重载+=操作符
-
string& operator+=(const char c); //重载+=操作符
-
string& operator+=(const string& str); //重载+=操作符
-
string& append(const char *s); //把字符串s连接到当前字符串结尾
-
string& append(const char *s, int n); //把字符串s的前n个字符连接到当前字符串结尾
-
string& append(const string &s); //同operator+=(const string& str)
-
string& append(const string &s, int pos, int n); //字符串s中从pos开始的n个字符连接到字符串结尾
string查找和替换
功能描述:
-
查找:查找指定字符串是否存在
-
替换:在指定的位置替换字符串
函数原型:
-
int find(const string& str, int pos = 0) const; //查找str第一次出现位置,从pos开始查找,返回位置从0开始计算,不存在则返回-1
-
int find(const char* s, int pos = 0) const; //查找s第一次出现位置,从pos开始查找
-
int find(const char* s, int pos, int n) const; //从pos位置查找s的前n个字符第一次位置
-
int find(const char c, int pos = 0) const; //查找字符c第一次出现位置
#include <iostream>
#include <string>
int main() {
std::string str = "Hello, World! Welcome to C++ programming.";
// 查找子字符串 "World" 的位置
size_t pos = str.find("World");
if (pos != std::string::npos) {
std::cout << "找到 \"World\" 的位置: " << pos << std::endl; // 输出 7
} else {
std::cout << "未找到 \"World\"" << std::endl;
}
// 查找字符 '!' 的位置
pos = str.find('!');
if (pos != std::string::npos) {
std::cout << "找到 '!' 的位置: " << pos << std::endl; // 输出 12
} else {
std::cout << "未找到 '!'" << std::endl;
}
// 从指定位置开始查找
pos = str.find("C++", 15);
if (pos != std::string::npos) {
std::cout << "找到 \"C++\" 的位置: " << pos << std::endl; // 输出 19
} else {
std::cout << "未找到 \"C++\"" << std::endl;
}
return 0;
}
-
int rfind(const string& str, int pos = npos) const; //查找str最后一次出现位置,从pos开始查找,返回位置从0开始计算,不存在则返回-1
-
int rfind(const char* s, int pos = npos) const; //查找s最后一次出现位置,从pos开始查找
-
int rfind(const char* s, int pos, int n) const; //从pos查找s的前n个字符最后一次位置
-
int rfind(const char c, int pos = 0) const; //查找字符c最后一次出现位置
#include <iostream>
#include <string>
int main() {
std::string str = "Hello, World! Welcome to C++ programming.";
// 查找子字符串 "Hello" 的位置(从后向前查找)
size_t pos = str.rfind("Hello");
if (pos != std::string::npos) {
std::cout << "找到 \"Hello\" 的位置: " << pos << std::endl; // 输出 0
} else {
std::cout << "未找到 \"Hello\"" << std::endl;
}
// 查找字符 '!' 的位置(从后向前查找)
pos = str.rfind('!');
if (pos != std::string::npos) {
std::cout << "找到 '!' 的位置: " << pos << std::endl; // 输出 12
} else {
std::cout << "未找到 '!'" << std::endl;
}
// 从指定位置开始查找
pos = str.rfind("C++", 25);
if (pos != std::string::npos) {
std::cout << "找到 \"C++\" 的位置: " << pos << std::endl; // 输出 19
} else {
std::cout << "未找到 \"C++\"" << std::endl;
}
return 0;
}
-
string& replace(int pos, int n, const string& str); //替换从pos开始n个字符为字符串str,n个字符会全部替换为str,字符串长度可能会减小或增加,由str长度决定
-
string& replace(int pos, int n, const char* s); //替换从pos开始的n个字符为字符串s
#include <iostream>
#include <string>
int main() {
std::string str = "Hello, World!";
// 替换从位置 7 开始的 5 个字符("World")为 "C++"
str.replace(7, 5, "C++");
std::cout << "替换后的字符串: " << str << std::endl; // 输出 "Hello, C++!"
// 替换从位置 7 开始的 3 个字符("C++")为 "Python"
str.replace(7, 3, "Python");
std::cout << "再次替换后的字符串: " << str << std::endl; // 输出 "Hello, Python!"
return 0;
}
string字符串比较
功能描述:
-
字符串之间的比较
比较方式:
-
字符串比较是按字符的ASCII码进行对比
-
相等返回 0
-
大于返回 1
-
小于返回 -1
-
比较方式就是逐字符对比,只要两个字符不相等即认为字符ASCII码值大的字符串大
函数原型:
-
int compare(const string &s) const; //与字符串s比较
-
int compare(const char *s) const; //与字符串s比较
字符串比较主要是比较两个字符串是否相等,比谁大谁小意义不大
string字符存取
string中单个字符存取方式有两种
-
char& operator[](int n); //通过[]取字符
-
char& at(int n); //通过at方法获取字符,直接返回对应位置字符
string插入和删除
功能描述:
-
对string字符串进行插入和删除字符操作
函数原型:
-
string& insert(int pos, const char* s); //插入字符串,在 pos 位置插入字符串,pos位置的字符会放到插入的字符串之后
-
string& insert(int pos, const string& str); //插入字符串
-
string& insert(int pos, int n, char c); //在指定位置插入n个字符c
#include <iostream>
#include <string>
int main() {
std::string str = "Hello, World!";
// 在位置 7 插入字符串 "C++ "
str.insert(7, "C++ ");
std::cout << "插入后的字符串: " << str << std::endl; // 输出 "Hello, C++ World!"
// 在位置 14 插入 3 个字符 '!'
str.insert(14, 3, '!');
std::cout << "再次插入后的字符串: " << str << std::endl; // 输出 "Hello, C++ !!!World!"
return 0;
}
-
string& erase(int pos, int n = npos); //删除从Pos开始的n个字符。如果位置超出范围,会抛出
std::out_of_range
异常。
#include <iostream>
#include <string>
int main() {
std::string str = "Hello, C++ World!";
// 删除从位置 7 开始的 4 个字符("C++ ")
str.erase(7, 4);
std::cout << "删除后的字符串: " << str << std::endl; // 输出 "Hello, World!"
// 尝试删除超出范围的字符
try {
str.erase(20, 5); // 位置 20 超出了字符串范围
} catch (const std::out_of_range& e) {
std::cout << "删除失败: " << e.what() << std::endl; // 输出异常信息
}
return 0;
}
string子串
功能描述:
-
从字符串中获取想要的子串
函数原型:
-
string substr(int pos = 0, int n = npos) const; //返回由pos开始的n个字符组成的字符串,字符串从0开始计算
string特殊查找
-
find_first_of
(const string &s) //查找字符串中第一个匹配任意字符的位置。
成功时返回字符的位置(从 0 开始),失败时返回 std::string::npos(可以视作-1)
#include <iostream>
#include <string>
int main() {
std::string str = "Hello, World!";
// 查找第一个匹配 'o' 或 'W' 的位置
size_t pos = str.find_first_of("oW");
if (pos != std::string::npos) {
std::cout << "找到第一个匹配字符的位置: " << pos << std::endl; // 输出 4(字符 'o')
} else {
std::cout << "未找到匹配字符" << std::endl;
}
// 查找第一个匹配 'x' 或 'y' 的位置(不存在)
pos = str.find_first_of("xy");
if (pos != std::string::npos) {
std::cout << "找到第一个匹配字符的位置: " << pos << std::endl;
} else {
std::cout << "未找到匹配字符" << std::endl; // 输出未找到
}
return 0;
}
-
find_last_of
(const string &s) //查找字符串中最后一个匹配任意字符的位置。
#include <iostream>
#include <string>
int main() {
std::string str = "Hello, World!";
// 查找最后一个匹配 'o' 或 'W' 的位置
size_t pos = str.find_last_of("oW");
if (pos != std::string::npos) {
std::cout << "找到最后一个匹配字符的位置: " << pos << std::endl; // 输出 8(字符 'W')
} else {
std::cout << "未找到匹配字符" << std::endl;
}
// 查找最后一个匹配 'x' 或 'y' 的位置(不存在)
pos = str.find_last_of("xy");
if (pos != std::string::npos) {
std::cout << "找到最后一个匹配字符的位置: " << pos << std::endl;
} else {
std::cout << "未找到匹配字符" << std::endl; // 输出未找到
}
return 0;
}
-
find_first_not_of
(const string &s) //查找字符串中第一个不匹配任意字符的位置。
#include <iostream>
#include <string>
int main() {
std::string str = "Hello,World!";
// 查找第一个不匹配 'Heo, Wrd!' 的位置
size_t pos = str.find_first_not_of("Heo, Wrd!");
if (pos != std::string::npos) {
// 输出 2(字符 'l'),因为str中没有l
std::cout << "找到第一个不匹配字符的位置: " << pos << std::endl; // 输出 12(字符 '!')
} else {
std::cout << "所有字符都匹配" << std::endl;
}
// 查找第一个不匹配所有字符的位置(不存在)
pos = str.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!,");
if (pos != std::string::npos) {
std::cout << "找到第一个不匹配字符的位置: " << pos << std::endl;
} else {
std::cout << "所有字符都匹配" << std::endl; // 输出所有字符都匹配
}
return 0;
}
-
find_last_not_of
(const string &s) //查找字符串中最后一个不匹配任意字符的位置。
#include <iostream>
#include <string>
int main() {
std::string str = "Hello,World!";
// 查找最后一个不匹配 'Helo, Wrd' 的位置
size_t pos = str.find_last_not_of("Heo, Wrd!");
if (pos != std::string::npos) {
// 输出 9(字符 'l'),因为str中没有l
std::cout << "找到最后一个不匹配字符的位置: " << pos << std::endl;
} else {
std::cout << "所有字符都匹配" << std::endl;
}
// 查找最后一个不匹配所有字符的位置(不存在)
pos = str.find_last_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!,");
if (pos != std::string::npos) {
std::cout << "找到最后一个不匹配字符的位置: " << pos << std::endl;
} else {
std::cout << "所有字符都匹配" << std::endl; // 输出所有字符都匹配
}
return 0;
}