#include <iostream>
#include <string>
using namespace std;
/*---------------------------------
18-28 18.28.判断string型字符串是否为空
18.29.swap_交换两个字符串的内容
18.30.将string型字符串转换为char型字符串
---------------------------------*/
int main()
{
cout<<"-------C++类 empty()-----------:"<<endl;
string str("");
if(str.empty()) //字符串查空
{ cout<<"字符串为空!"<<endl;}
else
cout<<str<<endl;
cout<<"-------C类 swab()-----------:"<<endl;
char ch1[15]="ofur";
char ch2[15]="";
swab(ch1,ch2,4); //以ch1的长度交换奇偶位字符后,拷贝到ch2
cout<<ch1<<endl<<ch2<<endl;
cout<<"-------C++类 swap()-----------:"<<endl;
string str1="ofur";
string str2="";
str1.swap(str2); //交换两个字符串
cout<<str1<<endl<<str2<<endl;
cout<<"-------C++类 c_str()-----------:"<<endl;
str1="hello world";
const char *ch;
ch=str1.c_str(); //将string型字符串转换为char型字符串
cout<<ch<<endl;
return 0;
}
运行结果:
-------C++类 empty()-----------:
字符串为空!
-------C类 swab()-----------:
ofur
foru
-------C++类 swap()-----------:
ofur
-------C++类 c_str()-----------:
hello world
Press any key to continue
C++ 字符串 10-- 18.28~30.string型字符串的查空empty、交换swap、类型转换c_str
最新推荐文章于 2023-12-17 16:56:33 发布