本篇继续来学习string这个类,一个string容器,里面就是一些C语言 char * c风格的字符组成。这里来学习下字符串的拼接功能,还有字符串查找和替换方法。
1.字符串拼接功能
在string中字符串拼接功能有下面几个函数,主要是重载+=操作符合使用append函数,尾部追加的意思
string& operator+=(const char* str); // 重载+=操作符
string& operator+=(const char c); // 重载+=操作符
string& operator+=(const string& str); // 重载+=操作符
string& apend(const char* s); // 把字符串s连接到当前字符串结尾
string& apend(const char* s, int n); // 把字符串s的前n个字符连接到当前字符串结尾
string& apend(const string &s); // 同operator+=(const string& str);
string& apend(const string &s, int pos, int n); //字符串s中从pos开始的n个字符连接到字符串结尾
下面使用代码分别来调用这些函数并打印输出测试。
#include <iostream>
#include <string>
using namespace std;
void test01()
{
//string& operator+=(const char* str); // 重载+=操作符
string str1 = "abc";
str1 += "def";
cout << "str1: " << str1 << endl;
//string& operator+=(const char c); // 重载+=操作符
string str2 = "abc";
str2 += 'd';
cout << "str2: " << str2 << endl;
//string& operator+=(const string& str); // 重载+=操作符
string str3 = "abc";
string str = "hello";
str3 += str;
cout << "str3: " << str3 << endl;
//string& apend(const char* s); // 把字符串s连接到当前字符串结尾
string str4 = str1.append(str3);
cout << "str4: " << str4 << endl;
//string& apend(const char* s, int n); // 把字符串s的前n个字符连接到当前字符串结尾
string str5 = str2.append("helloword", 4);
cout << "str5: " << str5 << endl;
//string& apend(const string &s); // 同operator+=(const string& str);
string str6;
str6.append(str5);
cout << "str6: " << str6 << endl;
//string& apend(const string &s, int pos, int n); //字符串s中从pos开始的n个字符连接到字符串结尾
string str7;
str7.append(str6,4, 3);
cout << "str7: " << str7 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
运行结果:
2.字符串查找方法
继续学习string的其他方法,现在来看看字符串查找方法
上面有find()和rfind()两种查找,find()是从左往右查找,而rfind()是从右往左查找,下面代码来测试下。
#include <iostream>
#include <string>
using namespace std;
void test01()
{
// 查找str第一次出现的位置。从pos开始查
string str1 = "hello world, hello China";
int index1 = str1.find("he", 0); // 默认开始位置是0,这里0参数可以不写
int index2 = str1.find("he", 2);
cout << "index1: " << index1 << endl;
cout << "index2: " << index2 << endl;
// 使用rfind查找
int index3 = str1.rfind("he");
cout << "index3: " << index3 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
运行结果
上面这么多查找,主要是从左到右还是从右到左查找顺序,然后又分查找字符串还是查找字符,从什么位置开始,前n个字符中最开始还是最后一个。注意查找返回类型是int,如果查找不存在,返回是-1,和java是一样。C++中字符串查找没有java那个lastIndexOf的查找方式,而是用rfind()来达到一样的效果。
4.字符串替换
接下来看看字符串替换的函数
这两个替换的函数,上面这个是C++风格的字符串替换,下面这个是C语言风格的字符串。我们用代码来测试第一个
这个函数有三个参数,开始替换位置,替换n个字符,需要替换成的str
#include <iostream>
#include <string>
using namespace std;
void test01()
{
// 替换字符串
string str1 = "hello world, hello China";
string str2 = str1.replace(0, 5, "Anthony");
cout << str2 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
运行结果
其实替换方法,还有其他重载的函数
replace() 函数的原型如下:
basic_string& replace (size_type p0, size_type n0, const E * s); //使用字符串 s 中的 n 个字符,从源串的位置 P0 处开始替换
basic_string& replace (size_type p0, size_type n0, const E *s, size_type n); //使用字符串 s 中的 n 个字符,从源串的位置 P0 处开始替换 1 个字符
basic_string& replace (size_type p0, size_type n0, const basic_string& str); //使用字符串 s 中的 n 个字符,从源串的位置 P0 处开始替换
basic_string& replace (size_type p0, size_type n0, const basic_string& str, size_type pos, size_type n); //使用串 str 的子串 str [pos, pos + n-1] 替换源串中的内容,从位置 p0 处开始替换,替换字符 n0 个
basic_string& replace (size_type p0, size_type n0, size_type n, E c); //使用 n 个字符 'c' 替换源串中位置 p0 处开始的 n0 个字符
basic_string& replace (iterator first0, iterator last0, const E * s);//使用迭代器替换,和 1) 用法类似
basic_string& replace (iterator first0, iterator last0, const E * s, size_type n);//和 2) 类似
basic_string& replace (iterator first0, iterator last0, const basic_string& str); //和 3) 类似
basic_string& replace (iterator first0, iterator last0, size_type n, E c); //和 5) 类似
basic_string& replace (iterator first0, iterator last0, const_iterator first, const_iterator last); //使用迭代器替换
第二种参数中用到了迭代器,我们前面在简单介绍了迭代器,大致看懂这里迭代器开始和迭代器结束这两个参数的,后面还需要继续学习其他容器,暂时不学习这种迭代器替换字符串的办法。
其实,很容易就发现C++这种字符串替换真的没有Java语言好用,在java中有str.replace("旧的字符串", "新的字符串"),这种其实使用更简便,但是C++中没有。