继续学习string类中的API,先看看字符串比较,然后学习字符串存取,最后看看字符串中如何插入和删除字符的函数。
1.字符串比较
在C++中string中字符串比较是使用函数compare,比较两个str的ASCII码表顺序。把字符串切割成一个一个字符,两个字符串从左到右相同位置上字符,一个一个比较,大小关系是ASCII编码表对于int数字的大小。例如字母a要比字母b编码排前面,所以a对应数字比字母b要小。
#include <iostream>
#include <string>
using namespace std;
void test01()
{
// 字符串比较
string str1 = "aello";
string str2 = "hello";
int res = str1.compare(str2);
if(res == 0)
{
cout << "str1等于str2" << endl;
}
else if(res == 1)
{
cout << "str1大于str2" << endl;
}
else
{
cout << res << endl;
cout << "str1小于str2" << endl;
}
}
int main()
{
test01();
system("pause");
return 0;
}
compare函数返回类型是int类型,有三种情况,如果相等,返回值是0,如果大于返回值是1,如果小于返回值是-1
所以,字符串比较大小是没有意义,一般就是比较是否等于0,也就是两个字符串是否相等。这里是因为单词还可以比较编码顺序,如果是中文呢,更没有比较大小的必要。上面可以把str1和str2改成你好,看看是否大于相等。
当然,字符串相等,也可以使用重载==符号,这种使用和java一样,可能更好理解,使用机会更多
#include <iostream>
#include <string>
using namespace std;
void test01()
{
// 字符串比较
string str1 = "你好";
string str2 = "你好";
if(str1 == str2)
{
cout << "str1和str2相等" << endl;
}
else
{
cout << "str1和str2不相等" << endl;
}
}
int main()
{
test01();
system("pause");
return 0;
}
2.字符串存取
在字符串的意思就是在一个字符串中,取出里面的某个位置的字符。在字符串中单个字符的存取有两个方法
char& operator[](int n);
char& at(int n);
测试代码
#include <iostream>
#include <string>
using namespace std;
void test01()
{
// 替换字存取
string str1 = "Hello World";
cout << str1[1] << endl;
cout << str1.at(1) << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
测试结果
接下来利用这两个方法来修改字符串中原来位置的字符,达到字符串保存新值的效果
#include <iostream>
#include <string>
using namespace std;
void test01()
{
// 替换字存取
string str1 = "Hello World";
str1[0] = 'C';
str1.at(2) = 'Z';
cout << str1 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
运行结果
3.字符串的插入和删除
接着学习字符串的插入和删除函数。插入和元素都需要给定元素所在原来字符串中的位置索引才能进行删除。当前前面学习的自定义数组的实现中尾插法和尾删法,默认都是元素最后一个元素位置进行操作。
字符串插入和删除原型函数:
string& insert(int pos, const char* s); // 插入字符串
string& insert(int pos, const string& str); // 插入字符串
string& insert(int pos, int n, char c); // 在指定位置插入n个字符c
string& erase(int pos, int n = npos); // 删除从pos开始的n个字符
下面我们代码来测试其中几个
#include <iostream>
#include <string>
using namespace std;
void test01()
{
// 字符串插入和删除
string str1 = "Hello World";
// 在pos位置后插入字符串
str1.insert(5, "xyz");
cout << str1 << endl;
// 在指定位置插入n个字符
str1.insert(5, 2, 'c');
cout << str1 << endl;
// 删除字符
str1.erase(2,2);
cout << str1 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
运行结果:
4.子串获取
子字符串也是字符串中常用的一个接口方法。
原型函数
string substr(int pos=0, int n=npos) const; // 返回由pos开始的n个字符串组成的字符串
传入两个参数,第一个是开始截取位置,第二个是要连续截取的长度。下面我们使用一个最简单的例子来看看效果
#include <iostream>
#include <string>
using namespace std;
void test01()
{
// 字符串获取子串
string str1 = "abcdef";
string str2 = str1.substr(1, 3);
cout << str2 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
运行结果
这个例子很简单,接下来我们来练习一个稍微比上面多一点点难度的例子。我们经常使用邮箱,我们的需求是从邮箱中获取用户的信息,简单来说既是获取邮箱地址中@这个字符前面的全部字符串内容。
#include <iostream>
#include <string>
using namespace std;
void test01()
{
// 字符串获取子串
string str1 = "anthony@abc.com";
// 需求是获取anthony这个字段,关键在于获取字符@的索引
int index1 = str1.find('@'); // 从左往右查找第一个符合的位置cls
if( index1 != -1)
{
string userInfo = str1.substr(0, index1); // 注意边界
cout << "user info: " << userInfo << endl;
}else
{
cout << "not a valid email format string" << endl;
}
}
int main()
{
test01();
system("pause");
return 0;
}
运行结果
注意上面17行代码substr(startIndex, 长度), 如果这个长度是通过字符串查找索引得到,这里不需要进行-1操作,索引是从0开始,刚好长度就是等于这个数值。