logo

C++ String append()

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=&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>

Primjer 2

Pogledajmo primjer dodavanja niza korištenjem položaja i duljine kao parametara.

 #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>

Primjer 3

Pogledajmo još jedan primjer.

 #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>