C++ string类使用详解

C++ string表示可变长的字符序列

需要的头文件  #include <string>     
而不是 #include <string.h>        
#include <string.h> 是C的头文件

string 定义在命名空间std中
所以加 using std::string; //不加这句 定义的每个string对象都得写成 std::string str;

  • 初始化方式
        string str = "abcdef";
        string str1("123");
        string str2 = "123";
        string str3 = str;
        string str4 = (str);
        string str5(3,'L'); //连续n个字符L组成
  • 判断是否为空
    empty()

  • 求实际大小
    size()

  • 判断是否相等
    ==
    !=

  • 累加
    str = str1 + str2;
    str= “hello” + “world”; //错误 必须保证 + 两侧至少一个是string类型

  • 取出字符串的值 -
    c_str()

  • 访问string中的字符 下标和迭代器 下面是下标法

int i = 0;
    int s_size = str.size();
    for (i;i<s_size;i++)
    {
        printf("%c\n",str[i]);
    }
  • 处理字符串中的单个字符


isalnum(c) 如果 c 是字母或数字,则为 True。
isalpha(c) 如果 c 是字母,则为 true。
iscntrl(c) 如果 c 是控制字符,则为 true
isdigit(c) 如果 c 是数字,则为 true。
isgraph(c) 如果 c 不是空格,但可打印,则为 true。
islower(c) 如果 c 是小写字母,则为 true。
isprint(c) 如果 c 是可打印的字符,则为 true。【注意:可打印的字符是指那些可以表示的字符】
ispunct(c) 如果 c 是标点符号,则 true。【注意:标点符号则是除了数字、字母或(可打印的)空白字符(如空格)以外的其他可打印字符】
isspace(c)如果 c 是空白字符,则为 true。【注意:空白字符则是空格、制表符、垂直制表符、回车符、换行符和进纸符中的任意一种】
isupper(c) 如果 c 是大写字母,则 true。
isxdigit(c) 如果是 c 十六进制数,则为 true。
tolower(c) 如果 c 大写字母,返回其小写字母形式,否则直接返回 c。
toupper(c)如果 c 是小写字母,则返回其大写字母形式,否则直接返回 c。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值