C++ string append()添加文本


C++ string append()添加文本

1.  C++ string append()添加文本

使用append()添加文本常用方法:

直接添加另一個完整的字符串:

如str1.append(str2);

添加另一個字符串的某一段子串:

如str1.append(str2, 11, 7);

添加幾個相同的字符:

如str1.append(5, '.');

注意,個數在前字符在后.上面的代碼意思為在str1后面添加5個".".

//========================================

#include<iostream>

using namespace std;

//========================================

int main()

{

    string str1="I like C++";

    string str2=",I like the world.";

    string str3="Hello";

    string str4("Hi");

    //====================================

    str1.append(str2);

    str3.append(str2, 11, 7);

    str4.append(5, '.');

    //====================================

    cout<<str1<<endl;

    cout<<str3<<endl;

    cout<<str4<<endl;

    system("pause");

    return 0;   

}

//========================================

運行結果為

I like C++,I like the world.

Hello World.

Hi.....


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM