😄1.resize
☀️2.append
🍰3.assign
🐶4.insert:
🐱5.erase
🦁6.replace
🦆7.find
🍓8.substr
😄1.resize
resize是重置空间的方法
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
void test1()
{
string s1("abcdef");
cout << s1.size() << endl;
cout << s1 << endl;
s1.resize(5,'x');
cout << s1.size() << endl;
cout << s1 << endl;
}
int main()
{
test1();
return 0;
}
☀️2.append
append方法是为string类对象添加字符串
void test2()
{
string s1;
s1.append("xxx");
cout << s1 << endl;
s1.append("abcd");
cout << s1 << endl;
}
int main()
{
test1();
test2();
return 0;
}
🍰3.assign
assign是替换字符串的功能。
void test3()
{
string s1 = "abcdef";
s1.assign("xx");
cout << s1 << endl;
}
🐶4.insert
insert在字符串某个位置插入字符串。
void test4()
{
string s1 = "abcdef";
s1.insert(2,"qw");
cout << s1 << endl;
}
🐱5.erase
erase是删除部分字符串。
void test5()
{
string s1 = "abxdef";
s1.erase(2);
cout << s1 << endl;
}
🦁6.replace
replace是从某个位置开始替换若干长度字符串。
void test6()
{
string s1 = "abcde";
s1.replace(1,3, "qw");
cout << s1 << endl;
}
🍓7.find
find是找到某个字符在字符串中第一个位置。
void test7()
{
string s1 = "abcdbf";
int i=s1.find('b',2);
cout << i << endl;
}
🦆8.substr
substr是截取一部分字符串保存下来。
void test8()
{
string s1 = "abcdefgh";
string s2=s1.substr(2,5);
cout << s2 << endl;
}
😄以上就是string类的基本常用方法,谢谢大家的支持!