1.C++中字符串有两种处理方式
1.1 C风格:
C语言中字符串的两个要素 首地址 '\0'
1.2 C++风格:
C++有string类 //暂时先当做一个类型来看
2.使用string类:
需要加命名空间 std: using namespace std;//加了std 不加string头文件也可以
需要加头文件<string> : #include <string> //一定不要写成 string.h
3.string类型的赋值及初始化
3.1单个变量时
string s1 = "hello";//初始化
string s2;
s2 = "hello";//赋值
string s3("hello");//初始化
3.2两个变量时
string s4 = s1;//用一个对象 初始化 另一个对象
string s5(s2);//用一个对象 初始化 另一个对象
string s6;
s6 = s3;//对象之间赋值
string s7 = s1+s2;//拼接赋值
string s8(5,'a');//五个a 不常用
例:
#include <iostream>
using namespace std;
#include <string>
int main(int argc, const char *argv[])
{
string s1 = "hello";//初始化
string s2;
s2 = "hello";//赋值
string s3("hello");//初始化
cout<<s1<<endl;
cout<<s2<<endl;
cout<<s3<<endl;
string s4 = s1;//用一个对象 初始化 另一个对象
string s5(s2);//用一个对象 初始化 另一个对象
string s6;
s6 = s3;//对象之间赋值 strcpy
string s7 = s1+s2;//拼接赋值
string s8(5,'a');//五个a 不常用
cout<<s4<<endl;
cout<<s5<<endl;
cout<<s6<<endl;
cout<<s7<<endl;
cout<<s8<<endl;
return 0;
}
4.C风格和C++风格字符串的转换
4.1 C++ --> C风格
//C++不推荐用C风格字符串,但是有些场景必须要这种风格的
//如 sprintf(dest,"%s%s",char * src1,char * src2)
转换方式 ---> c_str()函数
#include <iostream>
#include <cstdio>
using namespace std;
int main(int argc, const char *argv[])
{
char str[32] = "nihaobeijing";
string s1 = "hello world";
printf("%s\n",str);//正确的
//printf("%s\n",s1);//错误的 C++编译器 不认识
//可以通过 string.c_str() 函数来转换
//c_str() 函数返回值 就是C风格的字符串
printf("%s\n",s1.c_str());
return 0;
}
4.2 C风格 转 C++风格
C风格的字符串在C++中自动转换成C++风格的
例:
#include <iostream>
#include <cstdio>
using namespace std;
int main(int argc, const char *argv[])
{
string s1 = "hello";
string s2 = "hello";
if(s1 == "abcd"){//"abcd" 自动转换成C++ 风格 参与和s1 的比较
cout<<"YES"<<endl;
}else{
cout<<"NO"<<endl;
}
return 0;
}
5.string 类型的比较
== != > < >= <=
例:
#include <iostream>
#include <string>
using namespace std;
int main(int argc, const char *argv[])
{
string s1 = "heflo";
string s2 = "helao";
string s3 = "hello";
if(s1 > s2){//strcmp
cout<<"s1>s2"<<endl;
}else{
cout<<"s1<s2"<<endl;
}
if(s1 >= s3){
cout<<"s1>=s3"<<endl;
}else{
cout<<"s1<=s3"<<endl;
}
return 0;
}
6.两个常用函数
string.size() //计算字符串长度 不包括'\0'
string.empty() //判断字符串是否为 空 :空返回 真 非空返回 假
例:
#include <iostream>
using namespace std;
#include <string>
int main(int argc, const char *argv[])
{
string s1 = "hello";
string s2;
int ret = s1.size();//strlen
int ret2 = s1.empty();
cout<<ret<<" "<<ret2<<endl;
ret = s2.size();
//ret = s2.length();//length 函数和 size 一样
ret2 = s2.empty();
cout<<ret<<" "<<ret2<<endl;
return 0;
}
7.string 类型按位访问
通过 对象名[下标] 的方式访问
#include <iostream>
using namespace std;
#include <string>
int main(int argc, const char *argv[])
{
string s1 = "hello";
cout<<s1<<endl;
s1[1] = 'a';
s1.at(2) = 'm';//调用 at() 函数也可以 ---不常用
cout<<s1<<endl;
return 0;
}
8.string 类型的输入
cin 无法输入带有 空格 的字符串
可以使用 getline()函数来实现带空格字符串的输入
例如:
#include <iostream>
using namespace std;
#include <string>
int main(int argc, const char *argv[])
{
string s1;
getline(cin,s1);
cout<<s1<<endl;
return 0;
}
练习:
输入一个字符串,大写转小写,小写转大写,
其他字符全转成'-',并输出
#include<iostream>
#include<string>
using namespace std;
int main(int argc, char const *argv[])
{
string s1;
getline(cin,s1);
//C++支持 for 循环的第一个表达式中定义循环变量
//i 的作用域是for循环的{}
//生命周期是 for 循环结束
for(int i = 0; i < s1.size(); i++){
if('Z'>= s1[i] && s1[i] >= 'A'){
s1[i]+=32;
}else if('z'>= s1[i] && s1[i]>='a'){
s1[i]-=32;
}else{
s1[i] = '-';
}
}
cout<<s1<<endl;
return 0;
}