c++中string::function集合

string append()

  1.直接添加另一个完整的字符串:

   str1.append(str2);

  2.添加另一个字符串的某一段字串:

     str1.append(str2, 11,  7);  //添加str2中第11字符之后的7个字符

  3.添加n个相同的字符;

     str1.append(n, '-');  //在str1中添加n个“-”

 1 #include <iostream>
 2 using namespace std;
 3 
 4 int main()
 5 {
 6     string str1 = "I like c++";
 7     string str2 = "the so nice weather";
 8     string str3 = "hello";
 9     string str4("hello world");
10     
11     str1.append(str2);
12     str3.append(str2, 11, 7);   //重点
13     str4.append(5, '-');
14     
15     std::cout<<"str1 = "<<str1<<std::endl;
16     std::cout<<"str2 = "<<str2<<std::endl;
17     std::cout<<"str3 = "<<str3<<std::endl;
18     std::cout<<"str4 = "<<str4<<std::endl;
19     return 0;
20 }
-----输出:
  str1 = I like c++the so nice weather
  str2 = the so nice weather
  str3 = hello weathe
  str4 = hello world-----

string assign()

函数assign()常给string变量赋值;

1.直接用另一个字符串赋值

  str1.assign(str2);   //用str2给str1赋值;

2. 用另一个字符串的子串赋值

  str3.assign(str2, 4, 5);

3.用一个字符串的前一段子串赋值

   str4.assign("World", 5);

4.用几个相同的字符赋值

   str5.assign(10, 'c');

posted on 2013-12-23 19:55  阳台  阅读(298)  评论(0编辑  收藏  举报

导航