logo

C++ String Assign()

Denne funksjonen tildeler en ny verdi til strengen, og erstatter alt dens nåværende innhold.

Syntaks

Tenk på to strenger str1 og str2, syntaks vil være:

 Str1.assign(str2); 

Parametere

str : str er et strengobjekt, hvis verdi skal tilordnes.

hashing i datastruktur

subpos: Den definerer posisjonen til tegnet som skal kopieres som en understreng.

sublen: Den bestemmer antall tegn i strengen som skal kopieres i et annet strengobjekt.

n : Antall tegn som skal kopieres.

ch : Tegnverdi som skal kopieres n ganger

kompatibilitetstesting

Returverdi

*dette

Eksempel 1

La oss se et enkelt eksempel.

konvertere char til streng
 #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

La oss se et enkelt eksempel når posisjon og lengde er nevnt i parameterne.

 #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

La oss se et enkelt eksempel når n er gitt.

 #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

La oss se et enkelt eksempel når tegnverdi er gitt 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;>