string 类成员函数的简单示例(2)

// 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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值