标签:ppp logs char pac 索引 pen 第一个 asi his
一.string类头文件:#include <string>;using namespace std;
二.string类方法:
1.获取string的字符串长度:size(),size返回的字符串的长度不带‘\0‘.
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()函数:
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()函数的功能非常多:
7.删除字符串:
iterator erase( iterator pos ); iterator erase( iterator start, iterator end ); basic_string &erase( size_type index = 0, size_type num = npos ); |
erase()函数可以:
参数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 ); |
函数以下列方式赋值:
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()函数:
标签:ppp logs char pac 索引 pen 第一个 asi his
原文地址:https://www.cnblogs.com/yskn/p/9608229.html