Chinaunix首页 | 论坛 | 博客
  • 博客访问: 496945
  • 博文数量: 111
  • 博客积分: 3160
  • 博客等级: 中校
  • 技术积分: 1982
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-24 11:49
个人简介

低调、勤奋。

文章分类

全部博文(111)

文章存档

2014年(2)

2013年(26)

2012年(38)

2011年(18)

2010年(27)

分类: C/C++

2012-12-11 07:40:02

一、标准库string类型
string对象的常用操作:
1、s.empty() //是否为空串,是返回true
2、s.size();   //返回s中字符的个数,注意类型为string::size_type,而不是int
3、s[n]; 返回s中位置为n的字符,位置从0开始计算
4、s1 + s2; 把s1和s2连接成一个新的字符串,返回新生成的字符串
5、s1 = s2;//字符串替换,即先是否s1的内存,再按s2的大小申请内存,并将数据拷贝过去
6、v1 == v2; 比较v1和v2的内容,相当则返回true
7、!= , < , <=, and >= ;;实践

string对象和字符串字面值的连接。要求:操作数 + 左右必须有一个为string类型

如:

string s = “hello”;
string s2 = s + “world”;

点击(此处)折叠或打开

  1. #include <string>
  2. using std::string;
  3. #include <cctype>
  4. using std::isupper; //是否为大写字母
  5. using std::toupper; //转化为大写字母
  6. using std::islower; //是否为小写
  7. using std::tolower; // 转化为小写字母
  8. using std::isalpha; //是否为字母
  9. using std::isspace; //是否为空格
  10. using std::isalnum; //是否为字母或者数字
  11. using std::isdigit; //是否为数字
  12. using std::ispunct; //是否为标点符号
  13. #include <iostream>
  14. using std::cout; using std::endl;

  15. int main()
  16. {
  17.     string s("HELLO world!!!");
  18.     string::size_type punct_cnt = 0;

  19.     //s.size()返回的为string::size_type而不是int,一般为unsigned int 或者 unsigned long
  20.     //不像c,string可以直接通过size获取到字符串的长度
  21.     for (string::size_type index = 0; index != s.size(); ++index)
  22.     {
  23.         if (ispunct(s[index]))
  24.             ++punct_cnt;
  25.     }
  26.     //统计标点符号
  27.     cout << punct_cnt << " punctation characters is " << s << endl;

  28.     for (string::size_type index = 0; index != s.size(); ++index)
  29.     {
  30.         s[index] = tolower(s[index]);
  31.     }

  32.     cout << s << endl;

  33. }
阅读(872) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~