zoukankan      html  css  js  c++  java
  • 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');

  • 相关阅读:
    C语言 · 选择排序
    C语言 · 生物芯片
    C语言 · 猜灯谜
    C语言 · x的x次幂结果为10
    C语言 · LOG大侠
    C语言 · 成绩查询系统
    C语言 · C++中map的用法详解
    C语言 · 方程的解
    斯坦福大学公开课:监督学习应用,梯度下降
    斯坦福大学公开课:机器学习的动机与应用
  • 原文地址:https://www.cnblogs.com/chris-cp/p/3488015.html
Copyright © 2011-2022 走看看