#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
using namespace std;
void test1()//比较两个字符串
{
string s1;
s1.assign("abcdefg");//初始化,赋值
string s2 = "vbcdefg";//初始化,赋值
if (s1.compare(s2) == 0)//比较大小
{
cout << "S1等于S2" << endl;
}
else if (s1.compare(s2) > 0)
{
cout << "S1大于S2" << endl;
}
else
cout << "S1小于S2" << endl;
s1.append(s2);
cout << "拼接后的S1为:" <<s1<< endl;
}
void test2()//查找并将字符串中字符替换
{
string s1;
s1.assign("abcdefgde");//初始化
int p = s1.find("de");//查找de首次出现的位置,并返回下标,find()是从前往后找,rfind()是从后往前找
cout << "替换前S1为:" << s1 << endl;
cout << "test2字符串de出现的位置为:" << p << endl;
s1.replace(1, 3, "1111");//从第1个字符开始,将其及之后共三个
C++字符串string的读取、增删、查找、比较、截取子串、判断一个字符串中包含另一个字符串
最新推荐文章于 2024-03-03 11:20:56 发布