1.append方法: 在指定string尾部添加字串。
append方法的返回的结果为新string的引用。
常用的有6種重載:
a. basic_string &append(const basic_string &str);
将basic_string 类型的参数添加到原string的末尾
舉例:
string testappend = "Hello World";
testappend.append(" test");
cout<<testappend<<endl;
//打印结果为 Hello World test
b. basic_string &append( const char *str );
将char数组作为参数赋值给原string的末尾
举例:
string testappend = "Hello World";
char ch[10] = {' ', 't', 'e' ,'s' ,'t' ,'\0'};
testappend.append(ch);
cout<<testappend<<endl;
//打印结果为 Hello World test
c.basic_string &append( const basic_string &str, size_type index, size_type len );
将str字串从Index位置起,截取长度len,添加到原字串的末尾, index从1开始计算,而非0
举例:
string testappend = "Hello World";
testappend.append(" abcdefghijklmn", 6, 3);
cout<<testappend<<endl;
//打印结果为 Hello World efg
d. basic_string &append( const char *str, size_type num );
截取str字串首地址起的 num数量个字符,添加到原字串末尾
举例:
string testappend = "Hello World";
//直接使用string为第一个字串
testappend.append("???????????", 3);
//或者使用字符数组作为第一个参数,用str1或者&str1[0] 是相同的取数组首地址
char str1[20] = "1234567890";
testappend.append(str1, 6);
cout<<testappend<<endl;
//打印结果为 Hello World???123456
e. basic_string &append( size_type num, char ch );
将num数量的相同字符ch,添加到原字符串末尾
举例:
string testappend = "Hello World";
testappend.append(5, '5');
cout<<testappend<<endl;
//打印结果为 Hello World55555
6. basic_string &append( input_iterator start, input_iterator end );
把从iterator start开始的地方到iterator end结束的地方添加到原字符串的末尾
举例:
string testappend = "Hello World";
string it = "123456789";
testappend.append(it.begin()+5, it.end()-2);
cout<<testappend<<endl;
//打印结果是 Hello World67
--------------------------------------------------------------------------------
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 );
The current string content is extended by adding an additional appending string at its end.
The arguments passed to the function determine this appending string:
-
string& append ( const string& str );
- Appends a copy of str. string& append ( const string& str, size_t pos, size_t n );
-
Appends a copy of a substring of
str. The substring is the portion of
str that begins at the character position
pos and takes up to
n characters (it takes less than
n if the end of string is reached before).
If the position passed is past the end of str, an out_of_range exception is thrown.
string& append ( const char * s, size_t n );
- Appends a copy of the string formed by the first n characters in the array of characters pointed by s. string& append ( const char * s );
- Appends a copy of the string formed by the null-terminated character sequence (C string) pointed by s. The length of this character sequence is determined by the first ocurrence of a null character (as determined by traits.length(s)). string& append ( size_t n, char c );
- Appends a string formed by the repetition n times of character c. template<class InputIterator> string& append (InputIterator first, InputIterator last);
-
If
InputIterator is an integral type, behaves as the previous member function version, effectively appending a string formed by the repetition
first times of the character equivalent to
last.
In any other case, the parameters are considered iterators and the content is appended by the elements that go from element referred by iterator first to the element right before the one referred by iterator last.
Parameters
-
str
- Another object of class string whose content is entirely or partially appended to the content of this. pos
- Starting position of the substring in str that is appended. Notice that the first position has a value of 0, not 1. n
- Number of characters to append. s
-
Array with a sequence of characters.
In the third member version, the length is not determined by any occurrence of null-characters, but by parameter n.
By contrast, in the fourth member version, this is a null-terminated character sequence whose length is determined by the first occurrence of a null-character.
c
- Character value, repeated n times. start
- If along with last, both are integers, it is equivalent to parameter n, otherwise it is an iterator referring to the beginning of a sequence of characters. last
- If along with start, both are integers, it is equivalent to parameter c, otherwise it is an iterator referring to the past-the-end element of a sequence of characters.
Return Value
*this
Example
| |
Output:
Writing 10 dots here: .......... and then 5 more..... |