// string 类的常用基本操作
# include <iostream>
# include <string>
using namespace std;
int main(){
//s.empty() s如果为空则返回true,否者返回false。
string str_1 = "";
if(str_1.empty())
cout<<"the str_1 is empty."<<endl;
//s.size() 返回字符串的个数,即为字符串长度
string str_2 = "enjoy coding\n";
cout<<"the size of "<<"\""<<str_2<<"\""<<" is "<<str_2.size()
<<" characters, including the newline"<<endl;
//s1+s2 连接2个字符串,返回新字符串
string str_3 = "super", str_4 = "computer";
cout<<str_3+str_4<<endl;
//s.size() 返回值类型为 string::size_type
//string类和其他库类型都定义了一些配套类型(companion type)通过这些配套类型,库类的使用就能与机器无关(machine-independent).size_type 就是这些配套类型的一种。
//从string对象获取单个字符
string str_5 = "stranger";
for(string::size_type ix = 0; ix != str_5.size(); ++ix)
cout<<str_5[ix]<<" ";
cout<<endl;
//string对象中字符串的处理
//例如,通常需要知道某个特殊字符是否为空白字符,字母或数字,对string对象中字符串操作函数都包含在 cctype 头文件中。
string str_6 = "A16b2BEiefdf99";
for(ix = 0; ix != str_6.size(); ++ix){
if(isalpha(str_6[ix])) // isalpha(c) 如果c是字母,则为true
cout<<str_6[ix]<<" ";
}
cout<<endl;
for(ix = 0; ix != str_6.size(); ++ix){
if(isdigit(str_6[ix]))// isdigit(c) 如果c是数字,则返回true
cout<<str_6[ix]<<" ";
}
cout<<endl;
for(ix = 0; ix != str_6.size(); ++ix){
if(islower(str_6[ix]))// islower(c) 如果c是小写字母,则返回true
cout<<str_6[ix]<<" ";
}
cout<<endl;
for(ix = 0; ix != str_6.size(); ++ix){
if(isupper(str_6[ix])) // isupper(c) 如果c是大写字母,则返回true
cout<<str_6[ix]<<" ";
}
cout<<endl;
for(ix = 0; ix != str_6.size(); ++ix){
str_6[ix] = tolower(str_6[ix]);// 将大写字母转换成小写
cout<<str_6[ix];
}
cout<<endl;
for(ix = 0; ix != str_6.size(); ++ix){
str_6[ix] = toupper(str_6[ix]);
cout<<str_6[ix];
}
cout<<endl;
return 0;
}