STL string 容器

string 插入和删除

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

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

	//插入
	str.insert(1, "111");
	cout << str << endl;


	//删除
	str.erase(1, 3);
	cout << str << endl;
}

int main()
{
	test01();
	return 0;

}

string 查找和替换

/*
总结:
1:find查找时从左到右,rfind相反
2:找不到返回-1
3:replay在替换时,要指定从哪个位置器,多少个字符,替换成什么 字符
*/

#include<iostream>
using namespace std;

void test01()
{
	//查找
	string str1 = "sdfasffds";
	int pos = str1.find("de");//从左到右找
	if (pos == -1)
	{
		cout << "未找到字符串!" << endl;
	}
	else
	{
		cout << "找到字符串,pos=" << pos << endl;
	}

	//rfind

int pos1 =str1.rfind("f");//从右到左找
cout << pos1 << endl;

}


void test02()
{
	string str1 = "asfsfasfasf";
	str1.replace(1, 2, "2222");
	cout << "str1=" << str1 << endl;//从第一个位置开始替换4个2进去
}

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

	return 0;
}

string 赋值操作

#include<iostream>
using namespace std;
#include<string>
void test01()
{
	//1:char*类型字符串 赋值给当前的字符串
	string str1;
	str1 = "hello";
	cout << str1 << endl;

	//2:把字符串赋给当前的字符串
	string str2;
	str2 = str1;
	cout << str2 << endl;

	//3:字符赋值给当前的字符串
	string str3;
	str3 = 'a';
	cout << str3 << endl;

	
	string str4;
	str4.assign("hello C++");
	cout << str4 << endl;

	//5:把字符串s的前n个字符串赋给当前的字符串
	string str5;
	str5.assign("hello C++", 7);

	cout << str5 << endl;

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


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

	
	
	


}

int main()
{
	test01();
	return 0;
}

string 构造函数

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

void test01()
{  //创建一个空字符串--string()
	string s1;


  //使用字符串str初始化
	const char* str = "hello";
	string s2(str);

  //调用拷贝构造函数
	string s3(s2);

	//使用n个字符串a初始化
	string s4(19, 'a');
}

int main()
{
	test01();
	return 0;
}

string 字串

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

void test01()

{
	string str = "dfsfasf";
	string subStr = str.substr(1, 3);
	cout << subStr << endl;

}

void test02()
{
	string email = "zhangshan@sina.com";

	//从邮箱地址中,获取用户名

	int pos = email.find("@");
	cout << pos << endl;

	string serName = email.substr(0, pos);
	cout << serName << endl;

}

int main()
{
	test02();
	return 0;

}

string 字符串拼接

#include<iostream>
using namespace std;

#include<string>

void test01()
{

	string str1 = "我";

	str1 += "爱玩游戏";  //字符串追加
	cout << str1 << endl;

	str1 += ':';
	cout << str1 << endl;
	
	string str2 = "LOL DNF";//追加
	str1 += str2;

	cout << str1 << endl;


	string str3 = "I";//追加
	str3.append("love");
	cout << str3 << endl;

	str3.append("game addf", 4);
	//i love game
	cout << str3 << endl;

	str3.append(str2);
	cout << str3 << endl;

	str3.append(str2, 0, 3);//截取部分
	cout << str3 << endl;


}


int main()
{


	test01();


	return 0;
}

string 字符存取

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

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

//通过【】访问单个字符
	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] = 'c';
	str.at(1) = 'd';
	cout << str << endl;



}
int main()
{

	test01();

	return 0;

}

字符串比较

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

void test01()
{
	string str1 = "hello";
	string str2 = "hello";
	if (str1.compare(str2) == 0)
	{
		cout << "相等!" << endl;
	}
	else if(str1.compare(str2)>0)
	{
		cout << "str1大于str2" << endl;
	}
	else
	{
		cout<< "str1大于str2" << endl;
	}
}

int main()
{
	test01();
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值