2、string容器

1、string构造函数

#include<iostream>
using namespace std;
#include<string>

//string构造函数

//1.string();

//2.string(const char* s);

//3.string(const string& str);

//4.string(int n,char c);


void test01()
{
	//1.string();
	string s1;


	//2.string(const char* ch);
	const char* str = "hello word";
	string s2(str);

	cout << s2 << endl;


	//3.string(const string& str);
	string s3(s2);

	cout << s3 << endl;


	//4.string(int n,char ch);
	string s4(5, 'u');

	cout << s4 << endl;
}

int main()
{
	test01();

	system("pause");
	return 0;
}

2、string赋值操作

#include<iostream>
using namespace std;
#include<string>

//string赋值操作
//1.operator=(...);
//2.assign(...);

void test01()
{

	string str1;
	str1 = "hello word";
	cout << str1 << endl;


	string str2;
	str2 = str1;
	cout << str2 << endl;


	string str3;
	str3 = 'a';
	cout << str3 << endl;
}

void test02()
{
	string str4;
	str4.assign("hello word test02()");
	cout << str4 << endl;


	string str5;
	str5.assign("hello word test02()", 4);   //前4个赋值给str5
	cout << str5 << endl;


	string str6;
	str6.assign(str5);
	cout << str5 << endl;


	string str7;
	str7.assign(5, 'g');   //5个g赋值给str7
	cout << str7 << endl;
}

int main()
{
	test01();
	test02();

	system("pause");
	return 0;
}

3、string字符串的拼接

#include<iostream>
using namespace std;
#include<string>

//string字符串拼接
//1.string& operator+=(..);
//2.string& append(...);

void test01()
{
	string str1 = "我";

	//1.
	str1 += "是一个小猪猪";
	cout << str1 << endl;


	//2.
	str1 += 'G';
	cout << str1 << endl;

	string str3 = "这是什么?";

	//3.
	str1 += str3;
	cout << str1 << endl;
}

void test02()
{
	string str1 = "我是一个";

	//1.
	str1.append("小羊羊");
	cout << str1 << endl;

	//2.
	str1.append("who are you?", 3);
	cout << str1 << endl;
	
	string str2 = "这是我要追加的";

	//3.
	str1.append(str2);
	cout << str1 << endl;


	//4.
	string str3 = "我的追加操作";
	str3.append(str2, 2, 5);   //从str2 中,追加第2个字符开始的5个字符(英文字符)
	cout << str3 << endl;
}

int main()
{
	test01();
	test02();

	system("pause");
	return 0;
}

4、string字符串的查找和替换

#include<iostream>
using namespace std;

//字符串的查找替换

//1.查找
void test01()
{
	string str1 = "ABCDEFG";

	//1.find
	int pos1 = str1.find("CD");   //查找字符串CD
	int pos2 = str1.find("CF");

	cout << pos1 << endl;   //2,找到字符串,位置下标2
	cout << pos2 << endl;   //-1,未找到字符串


	//rfind
	//rfind 和 find 的区别
	//rfind从右往左找,find从左往右找

	//2.rfind
	int pos3 = str1.rfind("CD");
	cout << pos3 << endl;   //2
}

//2.替换
void test02()
{
	string str1 = "ABCDEFG";

	str1.replace(1, 3, "1111");   //把 1111 字符串替换进 BCD 的位置,1111共4个都替换了

	cout << str1 << endl;
}

int main()
{
	test01();
	test02();

	system("pause");
	return 0;
}

5、string字符串的比较

#include<iostream>
using namespace std;
#include<string>

//string字符串的比较---compare(主要是比较相等)
//按照ASCII码进行比较
//  = 返回 0
//  > 返回 1
//  < 返回 -1


void test01()
{
	string str1 = "hello";
	string str2 = "hello";

	if (str1.compare(str2) == 0)
	{
		cout << "str1 和 str2 相等" << endl;
	}

	string str3 = "hella";
	if (str1.compare(str3) > 0)
	{
		cout << "str1 比 str3 大" << endl;
	}

	string str4 = "hellz";
	if (str1.compare(str4) < 0)
	{
		cout << "str1 比 str4 小" << endl;
	}
}

int main()
{
	test01();

	system("pause");
	return 0;
}

6、string字符串存取

#include<iostream>
using namespace std;
#include<iostream>

//string字符存取

void test01()
{
	string str1 = "hello";

	cout << "str1 = " << str1 << endl;

	//1.str[i]
	for (int i = 0; i < str1.size(); i++)
	{
		cout << str1[i] << " ";
	}
	cout << endl;

	//2.str.at(i)
	for (int i = 0; i < str1.size(); i++)
	{
		cout << str1.at(i) << " ";
	}
	cout << endl;


	//3.修改
	str1[0] = 'A';
	cout << "str1 = " << str1 << endl;

	str1.at(1) = 'A';
	cout << "str1 = " << str1 << endl;
}

int main()
{
	test01();

	system("pause");
	return 0;
}
 

7、string的插入和删除

#include<iostream>
using namespace std;

//string的插入和删除

void test01()
{
	string str1 = "hello";
	cout << "str1 = " << str1 << endl;

	//1.插入
	str1.insert(1, "999");
	cout << "str1 = " << str1 << endl;

	//2.删除
	str1.erase(1, 3);  //第1个位置起,删除3个.  // 0 1 2 3 位置排序
	cout << "str1 = " << str1 << endl;
}

int main()
{
	test01();

	system("pause");
	return 0;
}

8、string截取子串

#include<iostream>
using namespace std;
#include<string>

//string 截取子串

void test01()
{
	string str1 = "ABCDEFG";

	//1.
	string substr = str1.substr(1, 3);  //第1个位置起,截取三个字符返回
	cout << "substr = " << substr << endl;
}


//实用的操作
void test02()
{
	string email = "zhangSan@qq.com";
	string email2 = "liSi@qq.com";

	int pos = email.find('@');
	int pos1 = email.find("@qq.com");
	int pos2 = email2.find("@qq.com");

	string name1 = email.substr(0, pos1);
	cout << "name1 = " << name1 << endl;

	string name2 = email2.substr(0, pos2);
	cout << "name2 = " << name2 << endl;
}


int main()
{
	test01();
	test02();

	system("pause");
	return 0;
}

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值