1.String::append与string::assign
string& append ( const string& str ); string& append ( const string& str, size_t pos, size_t n ); string& append ( const char* s, size_t n ); string& append ( const char* s ); string& append ( size_t n, char c ); template <class InputIterator> string& append ( InputIterator first, InputIterator last );
1)append(串,[位置],[长度])
2)append(字符数组,[长度])
3)append(个数,字符)
4)append(迭代器1,迭代器2)
2.string::at
const char& at ( size_t pos ) const; char& at ( size_t pos );
我们可以使用下标操作符[]和函数at()对元素包含的字符进行访问。但是应该注意的是操作符[]并不检查索引是否有效(有效索引0~str.length()),如果索引失效,会引起未定义的行为。而at()会检查,如果使用 at()的时候索引无效,会抛出out_of_range异常。
3.string::begin
1)begin() end() //提供类似STL的迭代器支持
string::iteratorit;it=str.begin();
2) rbegin() rend() //逆向迭代器
string::reverse_iteratorritrit=str.rbegin();
4.与大小长度有关的函数
1)string::capacity
2)string::length
3)string::size
4)string:max_size
5.string::clear
清空字符串
6string::compare
int compare ( const string& str ) const; int compare ( const char* s ) const; int compare ( size_t pos1, size_t n1, const string& str ) const; int compare ( size_t pos1, size_t n1, const char* s) const; int compare ( size_t pos1, size_t n1, const string& str, size_t pos2, size_t n2 ) const; int compare ( size_t pos1, size_t n1, const char* s, size_t n2) const;
1)copmare(字符数组|字符串)
2)compare(位置,长度,字符串,[位置],[长度])
3)compare(位置,长度,字符数组,[长度])
7.string::copy
可以字符串转化为字符数组
size_t copy ( char* s, size_t n, size_t pos = 0) const;
返回值是最后一个字符的位置的後一位,字符数组一般需要在最后一个位置填补'\0'
copy(字符数组,[长度],[位置])
int length; char buffer[20]; string str ("Test string..."); length=str.copy(buffer,6,5); buffer[length]='\0';
8.string::c_strstring::data
字符串转字符数组
char *cstr = new char [str.size()+1]; strcpy (cstr, str.c_str()); 9.string::empty 判断字符串是否为空
10.string::erase
string& erase ( size_t pos = 0, size_t n = npos ); iterator erase ( iterator position ); iterator erase ( iterator first, iterator last ); 1)erase([开始位置],[结束位置]) 2)erase(迭代器1,[迭代器2]) 11.string::find size_t find ( const string& str, size_t pos = 0 ) const; size_t find ( const char* s, size_t pos, size_t n ) const; size_t find ( const char* s, size_t pos = 0 ) const; size_t find ( char c, size_t pos = 0 ) const; 1)find(字符串,[位置]) 2)find(字符数组,[位置],[长度]) 3)find(字符,[位置]) string::rfind
size_t find ( const string& str, size_t pos = npos ) const; size_t find ( const char* s, size_t pos, size_t n ) const; size_t find ( const char* s, size_t pos = npos ) const; size_t find ( char c, size_t pos = npos ) const; |
1)find(字符串,[位置]) 2)find(字符数组,[位置],[长度]) 3)find(字符,[位置]) 12.string::insert
string& insert ( size_t pos1, const string& str ); string& insert ( size_t pos1, const string& str, size_t pos2, size_t n ); string& insert ( size_t pos1, const char* s, size_t n); string& insert ( size_t pos1, const char* s ); string& insert ( size_t pos1, size_t n, char c ); iterator insert ( iterator p, char c ); void insert ( iterator p, size_t n, char c ); template<class InputIterator> void insert ( iterator p, InputIterator first, InputIterator last ); |
1)insert(位置,字符串,[位置],[长度]) 2)insert(位置,字符数组,[长度]) 3)insert(位置,个数,字符) 4)insert(迭代器位置,[个数],字符) 5)insert(迭代器1,迭代器2)
13.string::replace
string& replace ( size_t pos1, size_t n1, const string& str ); string& replace ( iterator i1, iterator i2, const string& str ); string& replace ( size_t pos1, size_t n1, const string& str, size_t pos2, size_t n2 ); string& replace ( size_t pos1, size_t n1, const char* s, size_t n2 ); string& replace ( iterator i1, iterator i2, const char* s, size_t n2 ); string& replace ( size_t pos1, size_t n1, const char* s ); string& replace ( iterator i1, iterator i2, const char* s ); string& replace ( size_t pos1, size_t n1, size_t n2, char c ); string& replace ( iterator i1, iterator i2, size_t n2, char c ); template<class InputIterator> string& replace ( iterator i1, iterator i2, InputIterator j1, InputIterator j2 ); 1)replace(位置,长度,字符串,[位置],[长度]):replace(迭代器1,迭代器2,字符串) 2)replace(位置,长度,字符数组,[长度]): replace(迭代器1,迭代器2,字符数组,[长度]) 3)replace(位置,长度,个数,字符):replace(迭代器1,迭代器2,个数,字符) 4)? 14.string::resize oid resize ( size_t n, char c ); void resize ( size_t n );
Resizesthe string content to n characters.
If n issmaller than the current length of the string, the content is reducedto its first n characters, the rest being dropped.
If n isgreater than the current length of the string, the content isexpanded by appending as many instances of the c characteras needed to reach a size of n characters
15.string::substr
string substr ( size_t pos = 0, size_t n = npos ) const; substr([位置],[长度]) 16.string::swap void swap ( string& str );