logo

C++ String append()

Tato funkce se používá k rozšíření řetězce připojením na konec aktuální hodnoty.

Syntax

Zvažte řetězec str1 a str2. Syntaxe by byla:

 Str1.append(str2); Str1.append(str2,pos,len); Str1.append(str2,n); 

Parametry

str : Řetězcový objekt, který má být připojen k jinému řetězcovému objektu.

pozice: Určuje pozici prvního znaku, který má být připojen k jinému objektu.

jen : Počet znaků, které mají být zkopírovány do jiného řetězcového objektu jako podřetězec.

n : Počet znaků ke kopírování.

python __name__

Návratová hodnota

Tato funkce nevrací žádnou hodnotu.

Příklad 1

Podívejme se na příklad připojení řetězce do jiného objektu typu string.

 #include using namespace std; int main() { string str1=&apos;Welcome to C++ programming&apos;; string str2=&apos;language&apos;; cout&lt;<'before appending,string value is'<<str1<<'
'; str1.append(str2); cout<<'after appending, string return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> Before appending,string value is Welcome to C++ programming After appending,string value is Welcome to C++ programming language </pre> <h2>Example 2</h2> <p>Let&apos;s see the example of appending the string by using position and length as parameters.</p> <pre> #include using namespace std; int main() { string str1 = &apos;Mango is my favourite&apos; ; string str2 =&apos;fruit&apos;; cout&lt;<'before appending, string value is :' <<str1<<'
'; str1.append(str2,0,5); cout<<'after return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> Before appending, string value is Mango is my favourite After appending, string value is Mango is my favourite fruit </pre> <h2>Example 3</h2> <p>Let&apos;s see another example.</p> <pre> #include using namespace std; int main() { string str1 = &apos;Kashmir is nature&apos;; str1.append(&apos;of beauty&apos;,9) ; cout&lt;<'string value is :'<<str1; return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> String value is Kashmir is nature of beauty </pre> <br></'string></pre></'before></pre></'before>

Příklad 2

Podívejme se na příklad připojení řetězce pomocí pozice a délky jako parametrů.

 #include using namespace std; int main() { string str1 = &apos;Mango is my favourite&apos; ; string str2 =&apos;fruit&apos;; cout&lt;<\'before appending, string value is :\' <<str1<<\'
\'; str1.append(str2,0,5); cout<<\'after return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> Before appending, string value is Mango is my favourite After appending, string value is Mango is my favourite fruit </pre> <h2>Example 3</h2> <p>Let&apos;s see another example.</p> <pre> #include using namespace std; int main() { string str1 = &apos;Kashmir is nature&apos;; str1.append(&apos;of beauty&apos;,9) ; cout&lt;<\'string value is :\'<<str1; return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> String value is Kashmir is nature of beauty </pre> <br></\'string></pre></\'before>

Příklad 3

Podívejme se na další příklad.

 #include using namespace std; int main() { string str1 = &apos;Kashmir is nature&apos;; str1.append(&apos;of beauty&apos;,9) ; cout&lt;<\'string value is :\'<<str1; return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> String value is Kashmir is nature of beauty </pre> <br></\'string>