logo

C++ String Assign()

Ova funkcija dodjeljuje novu vrijednost nizu, zamjenjujući sav njegov trenutni sadržaj.

Sintaksa

Razmotrimo dva niza str1 i str2, Sintaksa bi bila:

 Str1.assign(str2); 

Parametri

str: str je string objekt, čiju vrijednost treba dodijeliti.

sesija je istekla

potpostavke: Definira položaj znaka koji se kopira kao podniz.

sublen : Određuje broj znakova niza koji će se kopirati u drugi objekt niza.

n : Broj znakova za kopiranje.

CH : Vrijednost znaka koja se kopira n puta

Povratna vrijednost

*ovaj

čvor java liste

Primjer 1

Pogledajmo jednostavan primjer.

 #include using namespace std; int main() { string str = &apos;javatpoint&apos;; string str1; str1.assign(str); cout&lt;<'assigned string is : ' <<str1; return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> Assigned string is :javatpoint </pre> <h2>Example 2</h2> <p>Let&apos;s see simple example when position and length are mentioned in the parameters.</p> <pre> #include using namespace std; int main() { string str = &apos;C is a programming language&apos;; string str1; str1.assign(str,7,20) ; cout&lt;<str1; return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> programming language </pre> <h2>Example 3</h2> <p>Let&apos;s see simple example when n is given.</p> <pre> #include using namespace std; int main() { string s; s.assign(&apos;javatpoint tutorial&apos;,10); cout&lt;<'assigned string is :' <<s; return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> Assigned string is : javatpoint </pre> <h2>Example 4</h2> <p>Let&apos;s see simple example when character value is given in a parameter.</p> <pre> #include using namespace std; int main() { string s; s.assign(10.&apos;a&apos;); cout&lt;<s; return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> aaaaaaaaaa </pre> <br></s;></pre></'assigned></pre></str1;></pre></'assigned>

Primjer 2

Pogledajmo jednostavan primjer kada su položaj i duljina spomenuti u parametrima.

korištenje interneta
 #include using namespace std; int main() { string str = &apos;C is a programming language&apos;; string str1; str1.assign(str,7,20) ; cout&lt;<str1; return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> programming language </pre> <h2>Example 3</h2> <p>Let&apos;s see simple example when n is given.</p> <pre> #include using namespace std; int main() { string s; s.assign(&apos;javatpoint tutorial&apos;,10); cout&lt;<\'assigned string is :\' <<s; return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> Assigned string is : javatpoint </pre> <h2>Example 4</h2> <p>Let&apos;s see simple example when character value is given in a parameter.</p> <pre> #include using namespace std; int main() { string s; s.assign(10.&apos;a&apos;); cout&lt;<s; return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> aaaaaaaaaa </pre> <br></s;></pre></\'assigned></pre></str1;>

Primjer 3

Pogledajmo jednostavan primjer kada je zadan n.

 #include using namespace std; int main() { string s; s.assign(&apos;javatpoint tutorial&apos;,10); cout&lt;<\'assigned string is :\' <<s; return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> Assigned string is : javatpoint </pre> <h2>Example 4</h2> <p>Let&apos;s see simple example when character value is given in a parameter.</p> <pre> #include using namespace std; int main() { string s; s.assign(10.&apos;a&apos;); cout&lt;<s; return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> aaaaaaaaaa </pre> <br></s;></pre></\'assigned>

Primjer 4

Pogledajmo jednostavan primjer kada je vrijednost znaka dana u parametru.

 #include using namespace std; int main() { string s; s.assign(10.&apos;a&apos;); cout&lt;<s; return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> aaaaaaaaaa </pre> <br></s;>