Ta funkcija se uporablja za razširitev niza z dodajanjem na koncu trenutne vrednosti.
Sintaksa
Razmislite o nizu str1 in str2. Sintaksa bi bila:
Str1.append(str2); Str1.append(str2,pos,len); Str1.append(str2,n);
Parametri
str: Objekt niza, ki ga je treba dodati drugemu objektu niza.
položaj: Določa položaj prvega znaka, ki bo pripet drugemu objektu.
samo: Število znakov, ki jih je treba kopirati v drug predmet niza kot podniz.
n: Število znakov za kopiranje.
kako inicializirati matriko v Javi
Povratna vrednost
Ta funkcija ne vrne nobene vrednosti.
Primer 1
Oglejmo si primer dodajanja niza v drug 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>
Primer 2
Oglejmo si primer dodajanja niza z uporabo položaja in dolžine kot parametrov.
#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>
Primer 3
Poglejmo še en primer.
#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>