C++ 标准库 string类型(三)
1,从string中获取字符
string虽然不是数组类型,但可以像数组一样,一个一个的获取字符,如下:
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
int main()
{
string s("Asia!!!!");
for(string::size_type x = 0;x < s.size();x++)
{
s[x] = 'A';
}
cout << s << endl;
return 0;
}
2,string对象中字符的处理
函数名 | 为如下参数时,返回值为真, 否则为零 |
isalpha() | 字母 |
isblank() | 标准的空白字符(空格、换行或水平制表符) 或任何其他预定义指定为空白符的字符 |
isdigit() | 阿拉伯数字 |
islower() | 小写字母 |
isupper() | 大写字母 |
isspace() | 空白字符:空格、换行、走纸、回车、垂直制表符、水平制表符 或任何其他预定义指定的字符 |
isalnum() | 字母;阿拉伯数字 |
iscntrl() | 控制字符 |
isascii() | ASCII码 |
isgraph() | 为除空格外的可打印字符 |
isprint() | 可打印字符(含空格) |
ispunct() | 标点符号 |
isxdigit() | 十六进制数字 |
函数名 | |
toascii() | toascii函数将字符c的高位清零,仅保留低七位。返回转换后的数值 |
tolower() | 若参数是大写字母,返回相应的小写字母,否则返回原始参数 |
toupper() | 若参数是小写字母,返回相应的大写字母,否则返回原始参数 |
下面举个简单的例子
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
int main()
{
string s("Asia!!!!");
bool R = false;
for(string::size_type x = 0;x < s.size();x++)
{
if(ispunct(s[x]))
R = true;
}
if(R)
cout << "字符串里有标点符号" << endl;
for(string::size_type y = 0;y < s.size();y++)
{
s[y] = toupper(s[y]);
}
cout << "转化为大写字母" << s << endl;
return 0;
}