C++ string容器 string容器

//int compare(const string& s) const;		//字符串比较
//int compare(const char* s) conts;		//与字符串s比较
#include <iostream>
#include <string>
using namespace std;
void test01()
{
	string s1 = "hello";
	string s2 = "aello";
	int ret = s1.compare(s2);
	if (ret == 0)
	{
		cout << "s1 == s2" << endl;
	}
	else if (ret > 0)
	{
		cout << "s1 > s2" << endl;
	}
	else
	{
		cout << "s1  < s2 " << endl;
	}
}
int main()
{
	test01();
	return 0;
}
//从字符串中获取想要的子串
//string substr(int pos = 0, int n = npos)const;	//返回由pos开始的n个字符组成的字符串
#include <iostream>
#include <string>
using namespace std;

void test0()
{
	string str = "abcdefg";
	string subStr = str.substr(1, 3);
	cout << "sunStr = " << subStr << endl; 

	string email = "hello@sina.com";
	int pos = email.find("@");
	string username = str.substr(0, pos);
	cout << "username: " << username << endl;
}
int main()
{
	test0();
	return 0;
}
#include <iostream>
#include <string>
using namespace std;
//char& operator[](int n);	//通过[]方式取字符
//char& at(int n);			//通过at方法获取字符
void test01() 
{
	string str = "Hello World";
	for (int i = 0; i < str.size(); i++)
	{
		cout << str[i] << " ";
	}
	cout << endl;
	for (int i = 0; i < str.size(); i++)
	{
		cout << str.at(i) << " ";
	}

	//由于返回值为引用,所以可以对源字符串进行修改
	str[0] = 'x';
	str.at(2) = 'o';
	cout << str << endl;
}
int main()
{
	test01();
	return 0;
}
//对tring字符串进行插入和删除操作
//string& insert(int pos, const char* s);	//插入字符串
//string& insert(int pos, const string& str);	//插入字符串
//string& insert(int pos, int n, char c);		//在指定位置插入n个字符c
//string& erase(int pos, int n = npos);			//删除从Pos位置开始的n个字符

#include <iostream>
#include <string>
using namespace std;
void test01()
{
	string str = "hello";
	str.insert(1, "111");
	cout << str << endl;
	str.erase(1, 3);	//从1号位置开始3个字符

	cout << str << endl;
}
int main()
{
	test01();
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值