目录
String类的使用进阶
String类对象求子串的操作
#include <iostream>
#include <string>
using namespace std;
int main()
{
// string类求子串功能
string str1 = "abcdefg";
cout << str1.substr(2) << endl; // 提取字符串中的2-end子字符串
cout << str1.substr(2, 4) << endl; // 提取字符串中的2-(2+4-1)子字符串
}
String类对象的查找操作
Find函数调用格式
int find(char c, int pos = 0) const; //从pos开始查找字符c在当前字符串的位置 |
int find(const char *s, int pos = 0) const; //从pos开始查找字符串s在当前串中的位置 |
int find(const char *s, int pos, int n) const; //从pos开始查找字符串s中前n个字符在当前串中的位置 |
int find(const string &s, int pos = 0) const; //从pos开始查找字符串s在当前串中的位置 |
//查找成功时返回所在位置,是个 >=0 的数,失败返回 string::npos 的值。npos 的值默认就是 -1 |
代码示例
#include <iostream>
#include <string>
using namespace std;
int main()
{
// string类求子串功能
string str1 = "abcdefg";
cout << str1.substr(2) << endl; // 提取字符串中的2-end子字符串
cout << str1.substr(2, 4) << endl; // 提取字符串中的2-(2+4-1)子字符串
// string类的查找功能
int CharPlace = str1.find('c', 2); // 从第2个字符开始查找'c'
cout << "'c'字符,位置为" << CharPlace << endl; // CharPlace=2
CharPlace = str1.find('c',2); // 默认从起始位置开始查找
cout << "'c'字符,位置为" << CharPlace << endl; // CharPlace=2
// 无论从哪个字符开始查找,只要能查找得到就返回该字符在字符串中的绝对地址
const char *p = "efg";
CharPlace = str1.find(p, 0, 2); // 从str1的第0个字符开始查找"ef"字符串
cout << "\"ef\"的位置为" << CharPlace << endl;
CharPlace = str1.find(p, 1); // 从str1的第1个字符开始查找"efg"字符串
cout << "\"efg\"的位置为" << CharPlace << endl;
CharPlace = str1.find("def", 1); //从str1的第一个字符开始找"def"字符串
cout << "\"def\"的位置为" << CharPlace << endl;
}
注:如果字符串内有多个重复的字符,find函数会返回第一个字符所在的位置。
String类中字符串的替换
Replace函数调用格式
string &replace(int p0, int n0, const char *s); //删除从p0开始的n0个字符,然后在p0处插入串s |
string &replace(int p0, int n0, const char *s, int n); //删除p0开始的n0个字符,然后在p0处插入字符串s的前n个字符 |
string &replace(int p0, int n0, const string &s); //删除从p0开始的n0个字符,然后在p0处插入串s |
string &replace(int p0, int n0, const string &s, int pos, int n); //删除p0开始的n0个字符,然后在p0处插入串s中从pos开始的n个字符 |
string &replace(int p0, int n0, int n, char c); //删除p0开始的n0个字符,然后在p0处插入n个字符c |
string &replace(iterator first0, iterator last0, const char *s); //把 [first0,last0)之间的部分替换为字符串s |
string &replace(iterator first0, iterator last0, const char *s, int n); //把 [first0,last0)之间的部分替换为s的前n个字符 |
string &replace(iterator first0, iterator last0, const string &s); //把 [first0,last0)之间的部分替换为串s |
string &replace(iterator first0, iterator last0, int n, char c); //把 [first0,last0)之间的部分替换为n个字符c |
string &replace(iterator first0, iterator last0, const_iterator first, const_iterator last); //把 [first0,last0)之间的部分替换成 [first,last)之间的字符串 |
代码示例
#include <iostream>
#include <string>
using namespace std;
int main()
{
// string类求子串功能
string str1 = "abcdefg";
cout << str1.substr(2) << endl; // 提取字符串中的2-end子字符串
cout << str1.substr(2, 4) << endl; // 提取字符串中的2-(2+4-1)子字符串
// string类的查找功能
int CharPlace = str1.find('c', 2); // 从第2个字符开始查找'c'
cout << "'c'字符,位置为" << CharPlace << endl; // CharPlace=2
CharPlace = str1.find('c',2); // 默认从起始位置开始查找
cout << "'c'字符,位置为" << CharPlace << endl; // CharPlace=2
// 无论从哪个字符开始查找,只要能查找得到就返回该字符在字符串中的绝对地址
const char *p = "efg";
CharPlace = str1.find(p, 0, 2); // 从str1的第0个字符开始查找"ef"字符串
cout << "\"ef\"的位置为" << CharPlace << endl;
CharPlace = str1.find(p, 1); // 从str1的第1个字符开始查找"efg"字符串
cout << "\"efg\"的位置为" << CharPlace << endl;
CharPlace = str1.find("def", 1); //从str1的第一个字符开始找"def"字符串
cout << "\"def\"的位置为" << CharPlace << endl;
// string类对象的替换功能
str1.replace(1, 3, "Hello", 0, 5); // 传入string类型的变量
cout << str1 << endl;
const char *ch1 = "World";
str1.replace(str1.find('o'), str1.size(), ch1, 5); // 传入指针型变量
cout << str1 << endl;
str1.replace(0, 1, 3, '6'); // 将str1中的0-1个字符替换成3个'6'
cout << str1 << endl;
// 用迭代器操作replace成员函数
string str2 = "你好";
string::iterator StringIterator = str1.begin() + 1;
str1.replace(StringIterator, str1.end(), str2.begin(), str2.end());
cout << str1 << endl;
str1.replace(str1.begin(), str1.end(), 3, '6');
cout << str1 << endl;
str1.replace(str1.begin(), str1.end(), "China 666");
cout << str1 << endl;
}
String类中字符串的插入
Insert函数的应用
string &insert(int p0, const char *s); |
|
string &insert(int p0, const char *s, int n); |
|
string &insert(int p0, const string &s); |
|
string &insert(int p0, const string &s, int pos, int n); | //前4个函数在p0位置插入字符串 s 中 pos 开始的前 n 个字符 |
string &insert(int p0, int n, char c); | //此函数在p0处插入n个字符c |
iterator insert(iterator it, char c); | //在it处插入字符c,返回插入后迭代器的位置 |
void insert(iterator it, const_iterator first, const_iterator last); | //在it处插入[first,last)之间的字符 |
void insert(iterator it, int n, char c); | //在it处插入n个字符c |
代码示例
#include <iostream>
#include <string>
using namespace std;
int main()
{
// string类求子串功能
string str1 = "abcdefg";
cout << str1.substr(2) << endl; // 提取字符串中的2-end子字符串
cout << str1.substr(2, 4) << endl; // 提取字符串中的2-(2+4-1)子字符串
// string类的查找功能
int CharPlace = str1.find('c', 2); // 从第2个字符开始查找'c'
cout << "'c'字符,位置为" << CharPlace << endl; // CharPlace=2
CharPlace = str1.find('c',2); // 默认从起始位置开始查找
cout << "'c'字符,位置为" << CharPlace << endl; // CharPlace=2
// 无论从哪个字符开始查找,只要能查找得到就返回该字符在字符串中的绝对地址
const char *p = "efg";
CharPlace = str1.find(p, 0, 2); // 从str1的第0个字符开始查找"ef"字符串
cout << "\"ef\"的位置为" << CharPlace << endl;
CharPlace = str1.find(p, 1); // 从str1的第1个字符开始查找"efg"字符串
cout << "\"efg\"的位置为" << CharPlace << endl;
CharPlace = str1.find("def", 1); //从str1的第一个字符开始找"def"字符串
cout << "\"def\"的位置为" << CharPlace << endl;
// string类对象的替换功能
str1.replace(1, 3, "Hello", 0, 5); // 传入string类型的变量
cout << str1 << endl;
const char *ch1 = "World";
str1.replace(str1.find('o'), str1.size(), ch1, 5); // 传入指针型变量
cout << str1 << endl;
str1.replace(0, 1, 3, '6'); // 将str1中的0-1个字符替换成3个'6'
cout << str1 << endl;
// 用迭代器操作replace成员函数
string str2 = "你好";
string::iterator StringIterator = str1.begin() + 1;
str1.replace(StringIterator, str1.end(), str2.begin(), str2.end());
cout << str1 << endl;
str1.replace(str1.begin(), str1.end(), 3, '6');
cout << str1 << endl;
str1.replace(str1.begin(), str1.end(), "China 666");
cout << str1 << endl;
// string类中字符串的插入
str1.insert(5, '6');
cout << str1 << endl;
const char *ch2 = "Chinese";
str1.insert(0, ch2, 7);
cout << str1 << endl;
string str3 = "北京欢迎你";
str1.insert(str1.begin(), str3.begin(), str3.end());
cout << str1 << endl;
}
String类中字符段的擦除
Erase函数的调用格式
iterator erase(iterator first, iterator last); | //删除 [first,last)之间的所有字符,返回删除后迭代器的位置 |
iterator erase(iterator it); | //删除it指向的字符,返回删除后迭代器的位置 |
string &erase(int pos = 0, int n = npos); | //删除pos开始的n个字符,返回修改后的字符串 |
代码示例
#include <iostream>
#include <string>
using namespace std;
int main()
{
// string类求子串功能
string str1 = "abcdefg";
cout << str1.substr(2) << endl; // 提取字符串中的2-end子字符串
cout << str1.substr(2, 4) << endl; // 提取字符串中的2-(2+4-1)子字符串
// string类的查找功能
int CharPlace = str1.find('c', 2); // 从第2个字符开始查找'c'
cout << "'c'字符,位置为" << CharPlace << endl; // CharPlace=2
CharPlace = str1.find('c',2); // 默认从起始位置开始查找
cout << "'c'字符,位置为" << CharPlace << endl; // CharPlace=2
// 无论从哪个字符开始查找,只要能查找得到就返回该字符在字符串中的绝对地址
const char *p = "efg";
CharPlace = str1.find(p, 0, 2); // 从str1的第0个字符开始查找"ef"字符串
cout << "\"ef\"的位置为" << CharPlace << endl;
CharPlace = str1.find(p, 1); // 从str1的第1个字符开始查找"efg"字符串
cout << "\"efg\"的位置为" << CharPlace << endl;
CharPlace = str1.find("def", 1); //从str1的第一个字符开始找"def"字符串
cout << "\"def\"的位置为" << CharPlace << endl;
// string类对象的替换功能
str1.replace(1, 3, "Hello", 0, 5); // 传入string类型的变量
cout << str1 << endl;
const char *ch1 = "World";
str1.replace(str1.find('o'), str1.size(), ch1, 5); // 传入指针型变量
cout << str1 << endl;
str1.replace(0, 1, 3, '6'); // 将str1中的0-1个字符替换成3个'6'
cout << str1 << endl;
// 用迭代器操作replace成员函数
string str2 = "你好";
string::iterator StringIterator = str1.begin() + 1;
str1.replace(StringIterator, str1.end(), str2.begin(), str2.end());
cout << str1 << endl;
str1.replace(str1.begin(), str1.end(), 3, '6');
cout << str1 << endl;
str1.replace(str1.begin(), str1.end(), "China 666");
cout << str1 << endl;
// string类中字符串的插入
str1.insert(5, 3, '6');
cout << str1 << endl;
const char *ch2 = "Chinese";
str1.insert(0, ch2, 7);
cout << str1 << endl;
string str3 = "北京欢迎你";
str1.insert(str1.begin(), str3.begin(), str3.end());
cout << str1 << endl;
// string类中字符段的擦除
str1.erase(str1.begin()); // 擦除一个字符
str1.erase(str1.begin() + 3, str1.end() - 2); // 擦除一段字符,用迭代器做索引
str1.erase(0, 3); // 擦除一段字符,用字符地址做索引
}
小练习
题目
将以下字符串以#号为分隔符进行分割,将分割后的各个子串的结果存储到一个 vector 中,字符串如下:123#ab##cctry.com#
代码示例
// 小练习.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
// 题目:小作业:
// 将以下字符串以#号为分隔符进行分割,将分割后的各个子串的结果存储到一个 vector 中,字符串如下:
// 123#ab##cctry.com#
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
string str1 = "123#ab##cctry.com#";
vector<string> StoreSubstr;
vector<int> SymbolNumber;
// 遍历求解'#'的个数和'#'的位置
int LastSymbol = str1.find_last_of('#');
for (int i = 0; i<= LastSymbol; i++)
{
if (str1[i] == '#')
{
SymbolNumber.push_back(i);
}
}
SymbolNumber.insert(SymbolNumber.begin(), 0);
// 将子串分别装入vector数组中
for (int i = 0; i < SymbolNumber.size() - 1; i++)
{
StoreSubstr.push_back(str1.substr(SymbolNumber[i], SymbolNumber[i + 1] - SymbolNumber[i] + 1));
cout << StoreSubstr[i] << endl;
}
}