
目录
1. 构造函数
2. 字符串赋值
3. 字符串拼接
4. 字符串查找和拼接
5. 字符串比较
6. 字符存取
7. 字符串插入和删除
1. 构造函数
C++98有以下主要构造函数
//string的构造函数
void test01()
{
//1.默认创造
string s1;
//2.字符串构造
string s2("hello,world");
cout << s2 << endl;
//3.拷贝构造
string s3(s2);
cout << s3 << endl;
//4.字符构造
string s4(10, 'a');
cout << s4 << endl;
//5.字符串构造
string s5("hello,world", 5);
cout << s5 << endl;
//6.拷贝构造
//从下标0开始拷贝5个字符的字符串
string s6(s2, 0, 5);
cout << s6 << endl;
}
int main()
{
test01();
return 0;
}
2. 字符串赋值
字符串赋值有两种 *** 作符:
1. = *** 作符
#include"String.h"
void test01()
{
//赋值 *** 作
//1. = *** 作符
//1. 字符串赋值
string s1;
s1 = "hello,world";
cout << "s1 = " << s1 << endl;
//2. string类赋值
string s2;
s2 = s1;
cout << "s2 = " << s2 << endl;
//3. 字符赋值
string s3;
s3 = 'a';
cout << "s3 = " << s3 << endl;
}
int main()
{
test01();
return 0;
}
2. assign方法
void test02()
{
//assign方法
//1.字符串赋值
string s1;
s1.assign("hello,world");
cout << "s1 = "<< s1 << endl;
s1.assign("hello,world", 5);
cout << "s1 = " << s1<< endl;
//2.类赋值
string s2;
s2.assign(s1);
cout << "s2 = " << s2 << endl;
//赋值一部分
s2.assign (s1, 0, 3);//从下标0开始,取3个字符的字符串进行赋值
cout << "s2 = " << s2 << endl;
//字符赋值
string s3;
s3.assign(10,'a');
cout << "s3 = " << s3 << endl;
}
int main()
{
test02();
return 0;
}
3. 字符串拼接
1. += *** 作符
//字符串拼接 *** 作
void test01()
{
//1.字符串拼接
string s1 = "I";
s1 += " love ";
cout << "s1 = " << s1 << endl;
//2.字符拼接
s1 += 'U';
cout << "s1 = " << s1 << endl;
//3. 类拼接
string s3 = " zhangsan";
s1 += s3;
cout << "s1 = " << s1 << endl;
}
int main()
{
test01();
return 0;
}
2. append方法拼接
//字符串拼接 *** 作
void test02()
{
//1.字符串拼接
string s1 = "小明";
s1.append(" 喜欢玩 ");
cout << "s1 = " << s1 << endl;
s1.append("LOL DNF", 3);//拼接当前字符串的前3个字符
cout << "s1 = " << s1 << endl;
//2.类拼接
string s2 = "小张和";
s2.append(s1);
cout << "s2 = " << s2 << endl;
string s3 = "小张";
s3.append(s1,5,100);
//从下标为5开始取100个字符,如果取的字符数大于字符串长度,则取的是字符串下标为5后的全部字符
cout << "s3 = " << s3 << endl;
//3.字符拼接
string s4 = "hh";
s4.append(10, 'h');
cout << "s4 = " << s4 << endl;
}
int main()
{
test02();
return 0;
}
4. 字符串查找和拼接
1. find方法
void test01()
{
//1.查找字符串
string s1 = "abdcdefgde";
cout <<"\"de\"第一次出现的位置 = "<< s1.find("de") << endl;
//从下标为0开始,寻找查询子串中的第一个字符的第一次出现位置
cout <<"\"de\"中的'd'第一次出现的位置 = "<< s1.find("de", 0, 1) << endl;
//查找字符
cout << "'g'第一次出现的位置 = "<< s1.find('g') << endl;
//查找类
string s2 = "def";
cout << "s2第一次出现的位置 = " << s1.find(s2) << endl;
}
int main()
{
test01();
return 0;
}
rfind与find语法类似,区别在于find是从左往右进行查找,而rfind是从右往左进行查找
2. replace方法
这里我们只考虑替换类型是字符串或者是类
void test02()
{
//2.替换
string s1 = "zhangsan";
//从下标0位置起,5个字符被替换成了"lisi"
s1.replace(0, 5, "lisi");
cout << s1 << endl;
string s2 = "wangwu";
//从下标0开始,8个字符被替换成s2中的字符串
s1.replace(0, 8, s2);
cout << s1 << endl;
}
int main()
{
test02();
return 0;
}
5. 字符串比较
compare方法
类似于C语言中的strcmp函数
void test01()
{
string s1 = "hello,world";
string s2 = "hello";
if (s1.compare(s2) == 0)
{
cout << "s1==s2" << endl;
}
else if (s1.compare(s2)>0)
{
cout << "s1 > s2" << endl;
}
else
{
cout << "s1 < s2" << endl;
}
}
int main()
{
test01();
return 0;
}
6. 字符存取
既然学习了这么多的 *** 作后,我们如果 *** 作字符串中的单个字符呢?
1. [ ] *** 作符
void test01()
{
string s1 = "hello";
for (int i = 0; i < s1.size(); i++)
{
cout << s1[i] << " ";
s[i]=i+1;
}
for (int i = 0; i < s1.size(); i++)
{
cout << s1[i] << " ";
}
}
int main()
{
test01();
return 0;
}
2. at()方法
void test01()
{
string s1 = "hello";
for (int i = 0; i < s1.length(); i++)
{
cout << s1.at(i) << " ";
s1.at(i) = i+1;
}
for (int i = 0; i < s1.length(); i++)
{
cout << s1.at(i) << " ";
}
}
int main()
{
test01();
return 0;
}
7. 字符串插入和删除
1. insert方法
void test01()
{
string s1 = "hello";
//1.插入 *** 作
//在下标为5插入字符串"world"
s1.insert(5, "world");
cout << "s1 = "<< s1 << endl;
}
int main()
{
test01();
return 0;
}
2. erase方法
void test01()
{
string s1 = "hello";
//1.插入 *** 作
s1.insert(5, "world");
cout << "s1 = "<< s1 << endl;
//2.删除操作
//从下标为5的位置开始删除两个字符
s1.erase(5, 2);
cout << "s1 = " << s1 << endl;
//插入和删除的起始下标都是0
}
int main()
{
test01();
return 0;
}
8.子串获取
substr方法
void test01()
{
string s1 = "hello,world";
//从下标0开始截取5个字符赋给s2
string s2 = s1.substr(0, 5);
cout << s2 << endl;
}
int main()
{
test01();
return 0;
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)