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