zoukankan      html  css  js  c++  java
  • string类的总结

    一.string类头文件:#include <string>;using namespace std;

    二.string类方法:

      1.获取string的字符串长度:size(),size返回的字符串的长度不带''.

      2.获取指定位置的子字符串:substr()(第一个参数是索引位置,第二个参数是子字符串的长度)

      3.在末尾添加字符串:append()  

        在字符串的末尾添加str,  s.apppend(str);

        在字符串的末尾添加str的子串,子串以index索引开始,长度为len  s.append(str,index,len)

        在字符串的末尾添加str中的num个字符, 在字符串的末尾添加num个字符ch,  s.append('a',5)

        在字符串的末尾添加以迭代器start和end表示的字符序列

      4.将字符串转化为字符串指针:c_str()

      5.在字符串中查找指定字符串:find:find(str,index,len)最后的参数可以不给出,返回str第一次出现的位置,当未找到返回string::npos

      size_type find( const basic_string &str, size_type index );
      size_type find( const char *str, size_type index );
      size_type find( const char *str, size_type index, size_type length );
      size_type find( char ch, size_type index );
    

      find()函数:

    • 返回str在字符串中第一次出现的位置(从index开始查找)如果没找到则返回string::npos,
    • 返回str在字符串中第一次出现的位置(从index开始查找,长度为length)。如果没找到就返回string::npos,
    • 返回字符ch在字符串中第一次出现的位置(从index开始查找)。如果没找到就返回string::npos

      6.插入字符串:insert()

      iterator insert( iterator i, const char &ch );
      basic_string &insert( size_type index, const basic_string &str );
      basic_string &insert( size_type index, const char *str );
      basic_string &insert( size_type index1, const basic_string &str, size_type index2, size_type num );
      basic_string &insert( size_type index, const char *str, size_type num );
      basic_string &insert( size_type index, size_type num, char ch );
      void insert( iterator i, size_type num, const char &ch );
      void insert( iterator i, iterator start, iterator end );
    

      insert()函数的功能非常多:

    • 在迭代器i表示的位置前面插入一个字符ch,
    • 在字符串的位置index插入字符串str,
    • 在字符串的位置index插入字符串str的子串(从index2开始,长num个字符),
    • 在字符串的位置index插入字符串str的num个字符,
    • 在字符串的位置index插入num个字符ch的拷贝,
    • 在迭代器i表示的位置前面插入num个字符ch的拷贝,
    • 在迭代器i表示的位置前面插入一段字符,从start开始,以end结束

      7.删除字符串:

      iterator erase( iterator pos );
      iterator erase( iterator start, iterator end );
      basic_string &erase( size_type index = 0, size_type num = npos );
    

    erase()函数可以:

    • 删除pos指向的字符, 返回指向下一个字符的迭代器,
    • 删除从start到end的所有字符, 返回一个迭代器,指向被删除的最后一个字符的下一个位置
    • 删除从index索引开始的num个字符, 返回*this.

    参数index  num 有默认值, 这意味着erase()可以这样调用:只带有index以删除index后的所有字符,或者不带有任何参数以删除所有字符.

      8.字符串赋值:

      basic_string &assign( const basic_string &str );
      basic_string &assign( const char *str );
      basic_string &assign( const char *str, size_type num );
      basic_string &assign( const basic_string &str, size_type index, size_type len );
      basic_string &assign( size_type num, char ch );
    

      函数以下列方式赋值:

    • 用str为字符串赋值,
    • 用str的开始num个字符为字符串赋值,
    • 用str的子串为字符串赋值,子串以index索引开始,长度为len
    • 用num个字符ch为字符串赋值.

      9.替换字符串:

      basic_string &replace( size_type index, size_type num, const basic_string &str );
      basic_string &replace( size_type index1, size_type num1, const basic_string &str, size_type index2,
      size_type num2 );
      basic_string &replace( size_type index, size_type num, const char *str );
      basic_string &replace( size_type index, size_type num1, const char *str, size_type num2 );
      basic_string &replace( size_type index, size_type num1, size_type num2, char ch );
      basic_string &replace( iterator start, iterator end, const basic_string &str );
      basic_string &replace( iterator start, iterator end, const char *str );
      basic_string &replace( iterator start, iterator end, const char *str, size_type num );
      basic_string &replace( iterator start, iterator end, size_type num, char ch );
    

      replace()函数:

    • 用str中的num个字符替换本字符串中的字符,从index开始
    • 用str中的num2个字符(从index2开始)替换本字符串中的字符,从index1开始,最多num1个字符
    • 用str中的num个字符(从index开始)替换本字符串中的字符
    • 用str中的num2个字符(从index2开始)替换本字符串中的字符,从index1开始,num1个字符
    • 用num2个ch字符替换本字符串中的字符,从index开始
    • 用str中的字符替换本字符串中的字符,迭代器start和end指示范围
    • 用str中的num个字符替换本字符串中的内容,迭代器start和end指示范围,
    • 用num个ch字符替换本字符串中的内容,迭代器start和end指示范围.
  • 相关阅读:
    CSS——清除浮动
    .net 多线程之线程取消
    .net 多线程临时变量
    NPOI helper
    添加学员存储过程
    SQL-sqlHelper001
    ado.net 中事务的使用
    T-SQL 事务2
    T-SQL 事务
    T-SQL 带参数存储过程
  • 原文地址:https://www.cnblogs.com/yskn/p/9608229.html
Copyright © 2011-2022 走看看