logo

C++ String Assign()

Denne funktion tildeler en ny værdi til strengen og erstatter alt dens nuværende indhold.

Syntaks

Overvej to strenge str1 og str2, syntaks ville være:

 Str1.assign(str2); 

Parametre

str : str er et strengobjekt, hvis værdi skal tildeles.

fjern første tegn i excel

underordnede: Den definerer placeringen af ​​tegnet, der skal kopieres som en understreng.

sublen: Det bestemmer antallet af tegn i streng, der skal kopieres i et andet strengobjekt.

n: Antal tegn, der skal kopieres.

ch: Tegnværdi skal kopieres n gange

spand sortering

Returværdi

*det her

Eksempel 1

Lad os se et enkelt eksempel.

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

Eksempel 2

Lad os se et simpelt eksempel, når position og længde er nævnt i parametrene.

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

Eksempel 3

Lad os se et simpelt eksempel, når n er givet.

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

Eksempel 4

Lad os se et simpelt eksempel, når tegnværdi er givet i en parameter.

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