C++字符和字符串

C++字符和字符串

一、单个字符:

1.语法:char ch=‘a’;
注意1:在显示字符型变量时,用单引号将字符括起来,不要用双引号
注意2: 单引号内只能有一个字符,不可以是字符串
2.C和C++中字符型变量只占用1个字节,字符型变量并不是把字符本身放到内存中存储,而是将对应的ASCII编码放入到存储单元

二、字符串类型:

  1. char 变量名[ ]=“字符串值”
    下面展示一些 代码
char str1[]=" hello world";
  1. string 变量名=" 字符串值";
string str2=" hello world";

三、string类型:

1、用string字符串进行拼接:
(1)string& append(const char *s) //把字符串s连接到当前字符串结尾
(2)string& append(const char *s,int n) //把字符串s的前n个字符连接到当前字符串结尾
(3)string& append(const string &s,int pos,int n) //字符串s从pos开始的n个字符连接到当前字符串结尾

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

//字符串拼接:
void test01()
{
    
    
cout<<"1.直接用+=替换拼接两个字符:"<<endl;
//直接用+=替换拼接两个字符
 string str1="我";
 str1+="爱学习";
 cout<<"str1="<<str1<<endl;

 cout<<"2.用append进行拼接操作:"<<endl;
 //用append进行拼接操作
 string str2="我爱学习:";
 str2.append("计算机操作系统");
 cout<<"str2="<<str2<<endl;

 str2.append("sf lkl离散数学",5);//英文字符和标点符号,空格算一个字节
 cout<<"str2="<<str2<<endl;
 str2.append("离散数学",6);//汉字算二个字节
 cout<<"str2="<<str2<<endl;

 string str3="123 56789";
 //从第6个字符开始截取,截几个字符
 str2.append(str3,5,2);//因为我们是从0开始索引
 cout<<"str2="<<str2<<endl;
}
int main()
{
    
    
	test01();
	system("pause");
	return 0;
}

在这里插入图片描述

2、用string字符串进行查找:
格式:a=str.find

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

//字符串查找和替换:
void test01()
{
    
    
//str.find查找第一次出现的位置
cout<<"find从左往右查"<<endl;
string str1="abcdefgde";
int position=str1.find("de");
//存在就返回它的位置,不存在就返回-1
cout<<"position="<<position<<endl;

cout<<"find从右往左查"<<endl;
int rposition=str1.rfind("de");
//存在就返回它的位置,不存在就返回-1
cout<<"rposition="<<rposition<<endl;
}
int main()
{
    
    
	test01();
	system("pause");
	return 0;
}


在这里插入图片描述
3、用string字符串进行替换:
格式:str.replace(a,b,“c”),从第a个位置开始到第b个位置,替换为c

#include <iostream>
#include <string>
using namespace std;
//字符串查找和替换:
void test01()
{
    
    
string str1="abcdefgde";//从0开始
//从第一个位置开始,将3个字符(bcd)替换为7。
str1.replace(1,3,"7");
cout<<"str1="<<str1<<endl;
}
int main()
{
    
    
	test01();
	system("pause");
	return 0;
}

在这里插入图片描述
4、用string字符串进行字符串比较:
(1)字符串比较是按字符的ASCII码进行对比
等于 返回0
大于 返回1
小于 返回-1
(2)字符串比较的主要目的是比较是否相等,判断谁大谁小无实际意义。

#include <iostream>
#include <string>
using namespace std;
//字符串查找和替换:
void test01()
{
    
    
string str1="zello world";
string str2="hello world";
string str3="3";
string str4="4";
if(str1.compare(str2)==0)
{
    
    
	cout<<"str1==str2"<<endl;
}
else if(str1.compare(str2)>0)
{
    
    
	cout<<"str1>str2"<<endl;
}
if(str3.compare(str4)<0)
{
    
    
	cout<<"str3<str4"<<endl;
}
}
int main()
{
    
    
	test01();
	system("pause");
	return 0;
}


在这里插入图片描述
5、用string字符串进行字符串存取:

#include <iostream>
#include <string>
using namespace std;
//字符串存取:
void test01()
{
    
    
  string str1="hello world";
  cout<<str1<<endl;
  cout<<str1[2]<<endl;
  str1[3]='x';//单个字符用单引号
  cout<<str1<<endl;
}
int main()
{
    
    
	test01();
	system("pause");
	return 0;
}


在这里插入图片描述
6、用string字符串进行插入删除:

#include <iostream>
#include <string>
using namespace std;
//字符串存取:
void test01()
{
    
    
  string str1="hello world";
  //插入
  str1.insert(1,"678");
  cout<<str1<<endl;
  //删除,从第一个位置开始删,删除3个
  str1.erase(1,3);//第一个参数是位置,第二个参数是个数
  cout<<str1<<endl;

  

}
int main()
{
    
    
	test01();
	system("pause");
	return 0;
}


在这里插入图片描述
7、获取想要的string字符串:
格式:string str2=str1.substr(1,3);

#include <iostream>
#include <string>
using namespace std;
//字符串获取:
void test01()
{
    
    
  string str1="hello world";
  //从字符串中获取想要的字符串用substr
  string str2=str1.substr(1,3);
  cout<<"str2="<<str2<<endl;
  }
void test02()
{
    
    
	string email="zhangsan@1517713674";
	int pos=email.find("@");
	string name=email.substr(0,pos);
	cout<<"name="<<name<<endl;
}
int main()
{
    
    
	test01();
	test02();
	system("pause");
	return 0;
}


在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_45960449/article/details/112767890
今日推荐