Ova se funkcija koristi za proširenje niza dodavanjem na kraj trenutne vrijednosti.
Sintaksa
Razmotrite string str1 i str2. Sintaksa bi bila:
Str1.append(str2); Str1.append(str2,pos,len); Str1.append(str2,n);
Parametri
str: String objekt koji treba dodati u drugi string objekt.
položaj: Određuje položaj prvog znaka koji treba dodati drugom objektu.
samo: Broj znakova koji se kopiraju u drugi objekt niza kao podniz.
n : Broj znakova za kopiranje.
koliko gradova ima u nas
Povratna vrijednost
Ova funkcija ne vraća nikakvu vrijednost.
Primjer 1
Pogledajmo primjer dodavanja niza u drugi objekt niza.
#include using namespace std; int main() { string str1='Welcome to C++ programming'; string str2='language'; cout<<'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'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 = 'Mango is my favourite' ; string str2 ='fruit'; cout<<'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's see another example.</p> <pre> #include using namespace std; int main() { string str1 = 'Kashmir is nature'; str1.append('of beauty',9) ; cout<<'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>
Primjer 2
Pogledajmo primjer dodavanja niza korištenjem položaja i duljine kao parametara.
#include using namespace std; int main() { string str1 = 'Mango is my favourite' ; string str2 ='fruit'; cout<<\'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's see another example.</p> <pre> #include using namespace std; int main() { string str1 = 'Kashmir is nature'; str1.append('of beauty',9) ; cout<<\'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>
Primjer 3
Pogledajmo još jedan primjer.
#include using namespace std; int main() { string str1 = 'Kashmir is nature'; str1.append('of beauty',9) ; cout<<\'string value is :\'<<str1; return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> String value is Kashmir is nature of beauty </pre> <br></\'string>
\'string>\'before>'before>