目录
1.string基本概念
2.string构造函数
3.string赋值操作
4.string字符串拼接
5.string查找和替换
6.string字符串比较
7.string字符存取
8.string插入和删除
9.string子串
本质:string是C++风格的字符串,而string本质上是一个类。
string和char*区别:
特点:string类内部封装了很多成员方法
例如:查找find,拷贝copy,删除delete 替换replace,插入insert
string管理char*所分配的内存,不用担心复制越界和取值越界等,由类内部进行负责
构造函数原型:
代码实现:
#include
using namespace std;
#include
void test01()
{
string s1;//默认构造
const char* str = "hello world!!!";
string s2(str);
cout << "s2 = " << s2 << endl;
string s3(s2);
cout << "s3 = " << s3 << endl;
string s4(10, 'a');
cout << "s4 = " << s4 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
赋值函数原型:
#include
using namespace std;
#include
void test01()
{
string str1;
str1 = "hello world";
cout << "str1 = " << str1 << endl;
string str2;
str2 = str1;
cout << "str2 = " << str2 << endl;
string str3;
str3 = 'a';
cout << "str3 = " << str3 << endl;
string str4;
str4.assign("hello C++");
cout << "str4 = " << str4 << endl;
string str5;
str5.assign("hello C++", 5);
cout << "str5 = " << str5 << endl;
string str6;
str6.assign(str5);
cout << "str6 = " << str6 << endl;
string str7;
str7.assign(10, 'w');
cout << "str7 = " << str7 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
拼接函数原型:
#include
using namespace std;
#include
void test02()
{
string str1 = "I";
str1 += " love game!";
cout << "str1 = " << str1 << endl;
str1 += ':';
cout << "str1 = " << str1 << endl;
string str2;
str2 = "LOL BEN";
str1 += str2;
cout << "str1 = " << str1 << endl;
string str3;
str3 = "我";
str3.append("爱玩");
cout << "str3 = " << str3 << endl;
str3.append("游戏放牛班", 4);
cout << "str3 = " << str3 << endl;
//str3.append(str2);
str3.append(str2, 2, 3);//从下标2的位置开始,截取3个字符,拼接到字符串的末尾
cout << "str3 = " << str3 << endl;
}
int main()
{
test02();
system("pause");
return 0;
}
查找和替换函数原型:
#include
using namespace std;
#include
//字符串查找和替换
//查找
void test03()
{
string str1 = "abcdefghde";
int pos = str1.find("de");
if (pos == -1)
{
cout << "没有找到字符串!" << endl;
}
else
{
cout << "找到字符串,pos = " << pos << endl;
}
//find和rfind的区别
//find从左往右查找,rfind从右往左查找
pos = str1.rfind("de");
cout << "pos = " << pos << endl;
}
//替换
void test04()
{
string str2 = "abcdefgh";
//从2号位置起3个字符替换为"11111"
str2.replace(2, 3, "11111");
cout << "str2 = " << str2 << endl;
}
int main()
{
test03();
test04();
system("pause");
return 0;
}
注:
字符串的比较是按字符的ASCII码进行比较:=返回0、>返回1、<返回-1。
比较函数原型:
#include
using namespace std;
//字符串比较
void test01()
{
string str1 = "hello";
string str2 = "xello";
if (str1.compare(str2) == 0)
{
cout << "str1 = str2" << endl;
}
else if (str1.compare(str2) > 0)
{
cout << "str1 > str2" << endl;
}
else
{
cout << "str1 < str2" << endl;
}
}
int main()
{
test01();
system("pause");
return 0;
}
注:字符串对比主要是用于比较两个字符串是否相等,判断谁大谁小的意义并不大。
string中单个字符的存取有两种方式:
#include
using namespace std;
#include
//字符存取
void test01()
{
string str1 = "hello";
cout << str1 << endl;
//1.通过[]访问单个字符
for (int i = 0; i < str1.size(); i++)
{
cout << str1[i] << " ";
}
cout << endl;
//2.通过at方式访问单个字符
for (int i = 0; i < str1.size(); i++)
{
cout << str1.at(i) << " ";
}
cout << endl;
//修改单个字符
str1[0] = 'x';
cout << "str1 = " << str1 << endl;
str1.at(1) = 'x';
cout << "str1 = " << str1 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
插入和删除函数原型:
#include
using namespace std;
#include
//字符串的插入和删除
void test()
{
string str = "hello";
cout << "str = " << str << endl;
//插入
str.insert(2, "456");
cout << "str = " << str << endl;
//删除
str.erase(5, 3);
cout << "str = " << str << endl;
}
int main()
{
test();
system("pause");
return 0;
}
注:插入和删除的起始下标都是从0开始的。
子串函数原型:
#include
using namespace std;
#include
//string求子串
void test01()
{
string str1 = "abcdefgh";
string str2 = str1.substr(2, 3);
cout << "str2 = " << str2 << endl;
}
//实用操作
void test02()
{
string emil = "[email protected]";
int pos = emil.find("@");
string userName = emil.substr(0, pos);
cout << "用户名:" << userName << endl;
}
int main()
{
test01();
test02();
system("pause");
return 0;
}