第一周学习C++string

一、string基本概念

本质:string是C++风格的字符串,而string本质上是一个类

string和char* 的区别:①char* 是一个指针②string是一个类,类内部封装了char* ,管理这个字符串是一个char*类型的容器

特点:string类内部封装了很多成员方法
例如:查找find,拷贝copy,删除delete,替换replace,插入insert
string管理char*所分配的内存,不用担心复制越界和取值越界等,由类内部进行负责

二、构造函数

原型:

  • string(str); //创建一个空的字符串
  • string(const char* s); //使用字符串s初始化
  • string(const string & str); //使用一个string对象初始化另一个 string对象
  • string(int n ,char c); //使用n个字符串c初始化

示例:

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string s;	 //创建空字符串
	cout << s << endl;
	
	const char*s1 = "hello world";
	string s2(s1); 
	cout << s2 << endl;
	
	string s3(s2);//调用构造函数s2 
	cout << s3 << endl;
	
	string s4(5,'A');//输出5个字符A
	cout << s4 << endl;
	
	return 0;
}

运行结果:
在这里插入图片描述

三、赋值操作

功能:对string字符串进行赋值

原型:

  • string & operator=(const char*s);//char*类型字符串赋值给当前字符串
  • string & operator=(const string & s);//把字符串s赋值给当前字符串
  • string & operator=(char c);//把字符c赋值给当前字符串
  • string & assign(const char*s);//char*类型字符串赋值给当前字符串
  • string & assign(const char*s , int n);//把字符串的前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 s;
	s = "hello world";//把字符串赋值给s
	cout << s <<endl;
	
	string s1;
	s1 = s;
	cout << s1 <<endl;
	
	string s2;
	s2 = 'a';
	cout << s2 <<endl;
	
	string s3;
	s3.assign("hello world");
	cout << s3 <<endl;
	
	string s4;
	s4.assign("hello world",5);//常量为选取的字符串个数,注意下标从0开始
	cout << s4 <<endl;
	
	string s5;
	s5.assign(s4);
	cout << s5 <<endl;
	
	string s6;
	s6.assign(5,'a');
	cout << s6 <<endl;
	
	return 0;
}

运行结果:
在这里插入图片描述

四、字符串拼接

功能:实现字符串末尾拼接

原型:

  • operate+=( );//括号里边可以是单个字符或字符串
  • append( );//括号里边可以是单个字符或字符串
  • append(const char*s,int n);//把字符串s的前n个字符拼接到当前字符串结尾
  • append(const string & s,int pos,int n);//字符串s从pos开始的n个字符拼接到字符串结尾

示例:

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string s="I";
	s+=" want to become";
	cout << s << endl;
	
	string s1="a programmer";
	s+=s1;
	cout << s << endl;
	
	string s2="I";
	s2.append(" like ");//接在原字符串的后面 
	s2.append("ACM asdf",3);//截取从下标为0开始到下标为3的位置 
	s2.append(s1,2,10);//从s1中下标为2开始截取10位 拼接到s2后面 
	cout << s2 << endl;
	
	return 0;
} 

运行结果:
在这里插入图片描述

五、查找和替换

功能:查找指定字符是否存在 & 在指定的位置替换字符

原型:
== - int find(const 字符或字符串,int pos=0)const;==//查找字符或字符串第一次出现的位置,从pos开始查找

  • int find(const char*s,int pos,int n)const;//从pos位置查找s的前n个字符第一次位置
  • int rfind(const 字符或字符串,int pos=npos)const;//查找字符或字符串最后一次出现的位置,从pos开始查找
  • int find(const char*s,int pos,int n)const;//从pos查找s的前n个字符最后一次位置
  • string & replace ( int pos,int n,const 字符串);//替换从pos开始n个字符为字符串

示例:

#include<iostream>
#include<string>
using namespace std;
int main()
{
	//查找
	string s="abcdefgfg";
	int pos=s.find("fg");
	if(pos==-1)
	{
		cout << "无" << endl; 
	}
	else
	{
		cout << pos << endl;
	}
	
	pos=s.rfind("fg");
	cout << pos << endl;//rfind是倒序查找即从后往前查找字符 
	
	//替换
	string s1="asdfghjk";
	s1.replace(2,3,"bbbb");//dfg替换为bbbb
	cout << s1 << endl;
	
	return 0;
} 

运行结果:
在这里插入图片描述

六、字符串比较

功能:比较字符串(按照字符的ASCII码进行比较,如果两个字符串相等,返回0;如果前者大,则返回1;后者大,则返回-1)

原型:

  • int compare(const string &s)const;//与字符串s比较
  • int compare(const char*s)const;//同上

示例:

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string s1="Hello";
	string s2="hello";
	int s=s1.compare(s2);
	if(s==0)
		cout << "s1 = s2" << endl;
	if(s>0)
		cout << "s1 > s2" << endl;
	if(s<0)
		cout << "s1 < s2" << endl;	
		
	return 0;
} 

运行结果:

七、字符存取&修改

string中单个字符存取方式有两种

  • ==char & operator [ ] ( int n ) ; ==//通过下标取字符
  • ==char at ( int n ) ; == //通过at取字符

示例:

#include<iostream>
#include<string>
using namespace std;
int main()
{
	//存取 
	string s="hello ACM";
	for(int i=0;i<s.size();i++)//size为字符串长度,length有同样功能
	{
		cout << s[i] <<" ";
	}
	cout << endl;
	
	for(int i=0;i<s.size();i++)
	{
		cout << s.at(i) <<" ";
	}
	cout << endl;
	
	//修改
	s[0]='#';
	s.at(1)='#';
	cout << s << endl; 
	
	return 0;
} 

八、插入和删除

功能:对string字符串进行插入或删除字符的操作

原型:

  • sting & insert ( int pos , const char *s ) ;
  • string & insert ( int pos , const string &str ) ;
  • string & insert ( int pos , int n , char c ) ; //在指定位置插入n个字符c
  • string & eraser (int pos , int n = npos ) ; //删除从pos开始的n个字符
#include<iostream>
#include<string>
using namespace std;
int main()
{
	string s="hello";
	s.insert(1,"234");//在下标为1的位置插入234
	cout << s << endl;
	
	s.erase(5,2);//从下标为5的位置往后删掉2个字符
	cout << s << endl;
	
	return 0;
} 

运行结果:
在这里插入图片描述

九、子串

功能:从字符串中获取想要的子串

原型:

  • string substr ( int pos = 0 , int n = npos ) const ; //返回从pos开始的n个字符组成的字符串

示例:

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string s="asdfg";
	string s1=s.substr(2,3);//从下标为2的位置开始往后获取3个字符的子串
	cout << s1 << endl;
	
	//可获取特定位置的字符串
	string email="asff@sina.com";
	int pos=email.find('@');
	string s2=email.substr(0,pos);
	cout << s2 << endl; 
	return 0;
} 

运行结果:
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值