string基本概念
string和char*区别:
- char*是一个指针
- string是一个类,内部封装了char*,是一个char*的容器
特点:
string内部封装了很多成员方法:查找find,拷贝copy,delete删除,替换replace等等
string管理char*所分配的内存,不用担心赋值越界和取值越界等,有类的内部负责
string构造函数
#include<iostream>
#include<vector>
#include<string>
using namespace std;
void test01()
{
string s1;
const char* str = "hello world";
string s2(str);
cout << s2 << endl;
string s3(s2);
cout << s3 << endl;
string s4(10, 'a');
cout << s4 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
string赋值操作
#include<iostream>
#include<vector>
#include<string>
using namespace std;
void test01()
{
string s1;
s1 = "hello world";
cout << s1 << endl;
string s2;
s2 = s1;
cout << s2 << endl;
string s3;
s3 = 'a';
cout << s3 << endl;
string s4;
s4.assign("hello,C++");
cout << s4 << endl;
string s5;
s5.assign("Hello,C++", 5);
cout << s5 << endl;
string s7;
s7.assign(10, 'w');
cout << s7<<endl;
}
int main()
{
test01();
system("pause");
return 0;
}
string字符串拼接
#include<iostream>
#include<vector>
#include<string>
using namespace std;
void test01()
{
string s1;
s1 = "我";
s1 += "爱玩游戏";
cout << s1 << endl;
s1 += '!';
cout << s1 << endl;
string s2;
s2 = "嘿嘿";
s1 += s2;
cout << s1 << endl;
string s3;
s3 = 'I';
s3.append("love");
cout << s3 << endl;
s3.append("game.ss", 6);
cout << s3 << endl;
s3.append(s2);
cout << s3 << endl;
s3.append(s1, 0, 4);
cout << s3 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
string查找和替换
#include<iostream>
#include<vector>
#include<string>
using namespace std;
void test01()
{
string s1="abcdefgde";
int ret = s1.find("de");
if (ret == -1)
{
cout << "没找到" << endl;
}
else
{
cout << ret << endl;
}
//rfind rfind从右往左查找 find从左往右查
ret = s1.rfind("de");
cout << ret << endl;
}
void test02()
{
string s1 = "sdasdasdasfwegersd";
s1.replace(1, 3, "ytiyui");
cout << s1;
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
string字符串比较
#include<iostream>
#include<vector>
#include<string>
using namespace std;
void test01()
{
string s1 = "hello world1";
string s2 = "hello world";
if (s1.compare(s2) == 0)
{
cout << "相等" << endl;
}
else if(s1.compare(s2) == 1)
{
cout << "da" << endl;
}
else
{
cout << "xiao" << endl;
}
}
int main()
{
test01();
system("pause");
return 0;
}
string字符存取
#include<iostream>
#include<vector>
#include<string>
using namespace std;
void test01()
{
string s1 = "hello world1";
string s2 = "hello world";
for (int i = 0; i < s1.size(); i++)
{
cout << s1[i] << " " ;
}
cout << endl;
for (int i = 0; i < s1.size(); i++)
{
cout << s1.at(i) << " ";
}
cout << endl;
for (int i = 0; i < s1.size(); i++)
{
s1[i] = i;
cout << s1.at(i) << " ";
}
}
int main()
{
test01();
system("pause");
return 0;
}
string插入和删除
#include<iostream>
#include<vector>
#include<string>
using namespace std;
void test01()
{
string s1 = "hello world1";
s1.insert(1, "asdsa");
cout << s1 << endl;
s1.erase(1, 5);
cout << s1 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
string子串
#include<iostream>
#include<vector>
#include<string>
using namespace std;
void test01()
{
string s1 = "hello world1";
string s2 = s1.substr(2, 7);
cout << s2 << endl;
}
//
void test002()
{
string email = "asdadafsfe3@qq.com";
int position = email.find('@');
string name = email.substr(0, position);
cout << name << endl;
}
int main()
{
test01();
test002();
system("pause");
return 0;
}