string基本概念:
本质:
string是c++风格的字符串,而string本质上是个类
string和char * 的区别:
char * 是一个指针
string是一个类,类内部封装了char * ,管理这个字符串,是一个char * 型的容器。
特点:
string类内部封装了很多成员方法
例如:
查找find,拷贝copy,删除delete,替换replace,插入insert,删除erase,比较compare
string构造函数:
#include<iostream>
using namespace std;
#include<string>
//string的构造函数
void test01()
{
string s1;//默认构造
const char* str = "hello word";
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;
}
string赋值操作:
#include<iostream>
using namespace std;
#include<string>
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;
}
void test02()
{
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, 'a');
cout << "str7=" << str7 << endl;
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
string字符串拼接:
#include<iostream>
using namespace std;
#include<string>
void test01()
{
string str1 = "hello";
str1 += " world";
cout << "str1=" << str1 << endl;
str1 += ';';
cout << "str1=" << str1 << endl;
string str2 = "aaa DNF";
str1 += str2;
cout << str1 << endl;
string str3 = "I";
str3.append("LOVE");
cout << str3 << endl;
str3.append("ppp ererer", 4);
cout << str3 << endl;
/*str3.append(str2);
cout << str3 << endl;*/
str3.append(str2,0,3);//参数2从哪个位置开始截取,参数3截取字符个数
cout << str3 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
string字符串比较:
#include<iostream>
using namespace std;
#include<string>
//字符串比较
void test01()
{
string str1 = "hello";
string str2 = "hello";
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<iostream>
using namespace std;
#include<string>
void test01()
{
string str = "hello";
//插入
str.insert(1,"1111");
cout << str << endl;
//删除
str.erase(1, 4);
cout << str << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
string字符存取:
#include<iostream>
using namespace std;
#include<string>
void test01()
{
string str = "hello";
//1.通过[]访问单个字符
for (int i = 0; i < str.size(); i++)
{
cout << str[i] << " ";
}
cout << endl;
//2.通过at方式访问单个字符
for (int i = 0; i < str.size(); i++)
{
cout << str.at(i) << " ";
}
cout << endl;
//修改单个字符
str[0] = 'x';
cout << str << endl;
str.at(1) = 'z';
cout << str << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
string子串:
#include<iostream>
using namespace std;
#include<string>
//string求子串
void test01()
{
string str = "abcdef";
string subStr = str.substr(1, 3);
cout << subStr << endl;
}
void test02()
{
string email = "hello@qq.com";
//从邮箱中获取用户信息
int pos = email.find("@");
string usrName = email.substr(0, pos);
cout << usrName << endl;
}
int main()
{
test01();
test02();
system("pause");
return 0;
}