标准库类型string

        标准库类型string表示可变长的字符序列,string本身是类,与容器操作相似,使用string类型必须首先包含#include<string>头文件。

       1、string 构造函数

                string s1;                                           string默认构造

                const char* str="hello world";        c风格字符串

                string s2(str);                                    将char*字符串拷贝给s2

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

                string s4(n,'value');                          将n个value赋s4

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

int main()
{
	string s1;
	const char* str = "hello world";
	string s2(str);
	string s3(s2);
	string s4(10, 'a');
	cout << "str : " << str << endl;
	cout << "s2 : " << s2 << endl;
	cout << "s3 : " << s3 << endl;
	cout << "s4 : " << s4 << endl;
	system("pause");
	return 0;
}

 

        2、string 容器赋值操作

                string& operator=(const char*s);                 char*类型字符串 赋值给当前的字符串

                string& operator=(const string &s);             把字符串s赋给当前的字符串

                string& operator=(char c);                            把字符赋给当前字符串

                string& assign(const char*s);                       把字符串s赋给当前的字符串

                string& assign(const char*s,int n);               把字符串s前n个字符串赋给当前的字符串

                string& assign(const string&s);                    把字符串s赋给当前字符串

                string& assign(int n ,char c);                        用n个字符串c赋给当前字符串

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

int main()
{
	string s1;
	s1 = "hello world";
	string s2;
	s2 = s1;
	string s3;
	s3 = 'c';
	string s4;
	s4.assign("hello world");
	string s5;
	s5.assign("hello world", 5);
	string s6;
	s6.assign(s2);
	string s7;
	s7.assign(5, 'c');
	cout << "s1 : " << s1 << endl;
	cout << "s2 : " << s2 << endl;
	cout << "s3 : " << s3 << endl;
	cout << "s4 : " << s4 << endl;
	cout << "s5 : " << s5 << endl;
	cout << "s6 : " << s6 << endl;
	cout << "s7 : " << s7 << endl;
	system("pause");
	return 0;
}

 

4、string 容器插入和删除

        insert(pos,"value");                                       在pos位置插入value值     

        erase(beg,end);                                              清除[beg,end]区间的字符,若只给beg一个值默认从beg开始全部清除;

        clear();                                                             清空容器

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

int main()
{
	string s1= "hello world";
	cout << "s1 : " << s1 << endl;
	s1.insert(3, "value");
	cout << "插入后s1 : " << s1 << endl;
	s1.erase(2, 4);
	cout << "指定区间s1 : " << s1 << endl;
	s1.erase(s1.begin()+2,s1.end()-4);
	cout << "利用迭代器指定区间s1 : " << s1 << endl;
	s1.erase(2);
	cout << "只给beg值s1 : " << s1 << endl;
	s1.clear();
	cout << "请空s1 : " << s1 << endl;
	system("pause");
	return 0;
}

 

  5、string容器拼接

        string& operator+=(const char*str);                重载+=操作符

        string& operator+=(const char c); 

        string& operator+=(const string& str); 

        append(const char* s);                             把字符串s连接到当前字符串尾部

        append(const char*s,int n);                      把字符串前n个字符连接到当前字符串尾部

        append(const string &s);                          把字符串s连接到当前字符串尾部

        append(const string &s,int pos,int n);     把字符串s从pos开始n个字符连接到当前字符串尾部

        

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

int main()
{
	string s1= "hello ";
	s1 += "world";
	cout << "s1 : " << s1 << endl;
	s1 += '!';
	cout << "s1 : " << s1 << endl;
	string s2="!6!6!6!";
	s1 += s2;
	cout << "s1 : " << s1 << endl;
	s1.append("yes");
	cout << "s1 : " << s1 << endl;
	s1.append("hhhhhhhhh", 2);
	cout << "s1 : " << s1 << endl;
	s1.append(s2);
	cout << "s1 : " << s1 << endl;
	s1.append(s2, 2, 2);
	system("pause");
	return 0;
}

 

  

6、字符串查找和替换

        find();                        从左至右

        rfind();                       从右至左

        replace(n,m,"字符串“);        从n号位置起m个字符替换成字符串

        

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

int main()
{
	string s1= "hello world";
	char c = s1.find(2);
	cout << "c : " << c << endl;
	char b = s1.rfind(2);
	cout << "b : " << b << endl;
	s1.replace(2, 2, "value");
	cout << "s1 : " << s1 << endl;
	system("pause");
	return 0;
}

 

7、字符串比较

        str1.compare(str2);                返回值    0相等,-1小于  ,1大于

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

int main()
{
	string s1= "hello world";
	string s2 = "helle w";
	int a;
	a = s1.compare(s2);
	cout << "a = " << a << endl;
	system("pause");
	return 0;
}

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

门三o

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值