01 string容器_构造函数

02 string容器_赋值操作

 03 string容器-字符串拼接

 04 string容器-字符串查找和替换

05 string容器_字符串比较

06 string容器_字符存取

07 string容器_插入和删除

08 string容器_子串


01 string容器_构造函数

#include<iostream>
using namespace std;


void test()
{
	string s1; //默认构造,创建空字符串,调用无参构造

	const char *str = "你好,世界";
	string s2(str); //把c_string转换成string
	cout <<"s2:" <<s2 << endl;

	string s3(s2); //调用拷贝构造
	cout << "s3:" << s3 << endl;

	string s4(10, 'a');//只能一个字符,打印十遍
	cout << "s4:" << s4 << endl;

}

int main()
{
	test();
	system("pause");
	return 0;
}

//总结"string的多种构造方式没有可比性,灵活使用即可

02 string容器_赋值操作

#include<iostream>
using namespace std;
#include<string>
void test01()
{

	string str1; //char*类型字符串赋值给当前的字符串
	str1 = "hollo world";
	cout << "str1=" << str1 <<endl;
	string str2; //把字符串s赋给当前的字符串
	str2=str1; 
	cout << "str2=" << str2 << endl;
	string str3;  //字符赋值给当前的字符串
	str3 = 'a';
	cout << "str3=" << str3 << endl;

	string str4; //把字符串s赋给当前的字符串
	str4.assign("hollo c++");
	cout << "str4=" << str4 << endl;
	string str5; //把字符串s的前n个字符赋给当前的字符串
	str5.assign("hollo c++", 5);
	cout << "str5=" << str5 << endl;

	string str6; //把字符串s赋给当前字符串
	str6.assign(str5);
	cout << "str6=" << str6 << endl;

	string str7; //用n个字符c赋给当前字符串
	str7.assign(10, 'w');
	cout << "str7=" << str7 << endl;
}

int main()
{
	test01();
	system("pause");
	return 0;
}

 03 string容器-字符串拼接

#include<iostream>
using namespace std;
void test01()
{
	string s1;
	s1 = "I";
	// I like computer
	s1 += " like computer";	 //重载+=操作符

	cout << "s1= "<<s1 << endl;
	string s2;
	s2 = " python c++";
	s1 += s2;
	cout << "s1= " << s1 << endl;
	string s3 = "I";
	s3.append(" love");
	//s3.append(s2); //把字符串s连接到当前字符串结尾
	//I love python
	//s3.append(s2, 0,7); //截取到python, 字符串s中从pos开始的n个字符连接到字符串结尾
	//I love c++
	s3.append(s2, 7, 4); //只截取 c++,参数2从哪个位置开始截取,参数3截取字符个数

	cout << "s3= " << s3 << endl;
	
	

}
int main()
{
	test01();

	system("pause");
	return 0;
}

 04 string容器-字符串查找和替换 

#include<iostream>
using namespace std;

//1.查找
void test01()
{
	string str1 = "jklgksdfjlsafjklsdjgkl";
	int pos = str1.find("gk");
	if (pos == -1)
	{
		cout << "没有找到" << endl;
	}
	else
	{
		cout << "用find找到了位置:"  <<pos << endl;
	}
	//rfind和find区别
	//rfind从右往左查找 find从左往右查找

	pos = str1.rfind("gk");
	cout << "用rfind找到位置:" << pos << endl;
}
//2.替换
void test02()
{
	string str2 = "jklsdfjlsafjklsdjgkl";

	//从1号位置起3个字符替换为"111111
	string str3 = str2.replace(1, 3, "111111");
	cout << str3 << endl;


}
int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}


//总结:
//find查找是从左往后,rfind从右往左
//find找到字符串后返回查找的第一个字符位置,找不到返回 - 1
//replace在替换时,要指定从哪个位置起,多少个字符,替换成什么样的字符串

05 string容器_字符串比较

#include<iostream>
using namespace std;

void test01()
{
	string str1 = "yang";
	string str2 = "yong";
	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;
}
//总结:字符串对比主要是用于比较两个字符串是否相等,判断谁大谁小意义并不是很大

06 string容器_字符存取

#include<iostream>
using namespace std;

void test01()
{
	string str = "yang yong sheng";
	//cout << str << endl;
	//1.通过[]访问单个字符
	cout << "方法一:通过[]访问单个字符" << endl;
	for (int i = 0; i < str.size(); i++)
	{
		cout <<str[i] << " ";
	}
	cout << endl;
	//1.通过at方式访问单个字符
	cout << "方法二:通过at方式访问单个字符" << endl;
	for (int i = 0; i < str.size(); i++)
	{
		cout <<str.at(i) << " ";
	}
	cout << endl;
	//1.修改字符
	str[0] = 'x';
	cout <<"替换第一位后:"<< str << endl;
	str[1] = 'x';
	cout << "替换第二位后:" << str << endl;

}
int main()
{
	test01();
	system("pause");
	return 0;
}

//总结:string 字符串中单个字符存取有两种方式,利用[]和at

07 string容器_插入和删除

#include<iostream>
using namespace std;

void test01()
{
	string str = "yang";
	//1.插入
	str.insert(1, "yong");
	cout << str << endl;
	//2.删除
	str.erase(1, 4); //从1号位置开始4个字符
	cout << str << endl;


}
int main()
{
	test01();
	system("pause");
	return 0;
}

//总结插入和删除起始下标都是从0开始的

08 string容器_子串

#include<iostream>
using namespace std;

void test01()
{
	string str = "yangyongsheng";
	string str2 = str.substr(0, 4);
	cout << str <<"截取后为" <<str2 << endl;
	string email = "yys34640040@outlook.com";
	int pos = email.find("@");
	cout << "@位置为" << pos << endl;
	string str3 = email.substr(0, pos);
	cout << email << "邮箱用户名为:" << str3 << endl;

}

int main()
{
	test01();
	system("pause");
	return 0;
}