zoukankan      html  css  js  c++  java
  • string 函数的用法

    头文件#include <string>

    1/赋值assign

      如:string s;

          string, s1(“123456”);

          s.assign(s1);//直接赋值,将s1的字符赋给s

          s.assign(s1, 3, 2);//s1中包括(下标为3的字符)在内的往后两个字符赋值给s

          s.assign(s1, 2, s1.npos);//s1中(下标为2的字符)直到末尾的字符全都赋给s

          s.assign(5, ‘X’);//5个‘X’字符赋给s

    2/获取字符串长度

      int len = s.size();

    3/字符串拼接

      string s4 = s3+s2;

    4/倒置串

      reverse(s.begin(), s.end());

    5/字符串比较

      s.compare(“good”);//  s与“good”进行比较,相等则返回0,字典序比good大则返回,小则返回-1

    6/string字符串末尾处的操作

      s.push_back(“abc”);//末尾添加

      s.pop_back();//末尾删除

    7/对(字符)进行处理

      头文件#include <ctype>

      isalnum(c);//如果c为字母或者数字,返回true

      isalpha(c);//如果c为字母,返回true

      iscntrl(c);//如果c为控制符,返回true

      isdigit(c);//如果c为数,返回true

      isgraph(c);//如果c不是空格,返回true

      islower(c);//如果c是小写字母,返回true

      isupper(c);//如果c是大写字母,返回true

      tolower(c);//如果c是大写字母,则返回小写字母

      toupper(c);//如果c是小写字母,则返回大写字母

    8/用迭代器操作

      s.insert(id, ”p”);//把字符串p插入到下标为id的位置

      s.insert(p, n, t);//  p下标之前插入nt的副本

      s.insert(p, cp, len);//在下标p之前插入cp数组的前len个元素

    9/删除元素

      s.erase(3);//删除下标为3的那个元素

      s.erase(0, 4);//删除从下标0到下标4的元素

      s.clear();//删除全部字符

    10/查找字符串

      s.find(“cat”);//找到第一个出现字符串“cat”的位置,返回“c”的下标,查不到则返回4294967295

  • 相关阅读:
    iOS开发UIKit框架-可视化编程-XIB
    ios swift例子源码网址总结
    两个单链表相交问题
    库函数atoi
    C++类型萃取
    【Swift】iOS导航栏错乱的原因
    【Swift】iOS裁剪或者压缩后出现的白边问题
    Linux进程调度分析
    【scala】可变与不可变的理解
    【scala】模式匹配
  • 原文地址:https://www.cnblogs.com/RootVount/p/10896431.html
Copyright © 2011-2022 走看看