logo

C++ String Assign()

Ta funkcija nizu dodeli novo vrednost in nadomesti vso njegovo trenutno vsebino.

Sintaksa

Razmislite o dveh nizih str1 in str2, sintaksa bi bila:

 Str1.assign(str2); 

Parametri

str: str je objekt niza, katerega vrednost je treba dodeliti.

t ff

podstavki: Določa položaj znaka, ki ga je treba kopirati kot podniz.

sublen : Določa število znakov niza, ki jih je treba kopirati v drug objekt niza.

n: Število znakov za kopiranje.

ch: Vrednost znaka, ki jo je treba kopirati n-krat

preverjanje ničelnosti v Javi

Povratna vrednost

*to

Primer 1

Poglejmo preprost primer.

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

Primer 2

Poglejmo preprost primer, ko sta položaj in dolžina omenjena v parametrih.

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

Primer 3

Poglejmo preprost primer, ko je podan 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>

Primer 4

Oglejmo si preprost primer, ko je vrednost znaka podana v 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;>