C++字符串string的学习

C++字符串string的使用
有关string的C++库函数,有很多对字符串操作相关的库函数,这里主要是学习一些个别对字符串操作的方法,在以后的使用中可以通过查询库函数的方法来查询相关的对字符串操作的函数,相关的网站有http://www.cplusplus.com/reference/ https://zh.cppreference.com/w/%E9%A6%96%E9%A1%B5
示例分析:

#include<iostream>
	#include<string>		//包含string头文件
	using namespace std;
	int main(void)
	{
		string s1, s2, s3;
		s1 = "hello cpp";
		s2 = "hello world";
		cout << "s1 is: " << s1 << endl;
		cout << "s2 is: " << s2 << endl;
		if(s3.empty())		//empty判断字符串变量是否为空
		{
			cout << "s3 is empty" << endl;
		}
		else
		{
			cout << "s3 is no empty" << endl;
		}
		cout << "s1'length is " << s1.length() << endl;		//length函数返回字符串的长度
		//s3 = s1.append(s2);		//append在字符串s1后连接s2字符串
		//s3 = s1 + s2;				//也是连接的作用,这里和python一样可以通过 + 的重载来实现字符串的连接
		s3 += s1;
		cout << "s3 is: " << s3 << endl;
		s2.insert(0,2,'s');		//insert用于插入字符,里面涉及到的参数形式有多种,注意:这里的示例插入的字符要用''而不能使用""
		cout << "after insert, s2 is: " << s2 << endl;
		
		return 0;
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值