logo

C++-Strings

In C++ ist String ein Objekt von std::string Klasse, die eine Zeichenfolge darstellt. Wir können viele Operationen an Zeichenfolgen durchführen, z. B. Verkettung, Vergleich, Konvertierung usw.


C++-String-Beispiel

Sehen wir uns das einfache Beispiel einer C++-Zeichenfolge an.

 #include using namespace std; int main( ) { string s1 = &apos;Hello&apos;; char ch[] = { &apos;C&apos;, &apos;+&apos;, &apos;+&apos;}; string s2 = string(ch); cout&lt;<s1<<endl; cout<<s2<<endl; } < pre> <p>Output:</p> <pre> Hello C++ </pre> <hr> <h2>C++ String Compare Example</h2> <p>Let&apos;s see the simple example of string comparison using strcmp() function.</p> <pre> #include #include using namespace std; int main () { char key[] = &apos;mango&apos;; char buffer[50]; do { cout&lt;&gt;buffer; } while (strcmp (key,buffer) != 0); cout&lt;<'answer is correct!!'<<endl; return 0; } < pre> <p>Output:</p> <pre> What is my favourite fruit? apple What is my favourite fruit? banana What is my favourite fruit? mango Answer is correct!! </pre> <hr> <h2>C++ String Concat Example</h2> <p>Let&apos;s see the simple example of string concatenation using strcat() function.</p> <pre> #include #include using namespace std; int main() { char key[25], buffer[25]; cout &lt;&lt; &apos;Enter the key string: &apos;; cin.getline(key, 25); cout &lt;&lt; &apos;Enter the buffer string: &apos;; cin.getline(buffer, 25); strcat(key, buffer); cout &lt;&lt; &apos;Key = &apos; &lt;&lt; key &lt;&lt; endl; cout &lt;&lt; &apos;Buffer = &apos; &lt;&lt; buffer&lt;<endl; return 0; } < pre> <p>Output:</p> <pre> Enter the key string: Welcome to Enter the buffer string: C++ Programming. Key = Welcome to C++ Programming. Buffer = C++ Programming. </pre> <hr> <h2>C++ String Copy Example</h2> <p>Let&apos;s see the simple example of copy the string using strcpy() function.</p> <pre> #include #include using namespace std; int main() { char key[25], buffer[25]; cout &lt;&lt; &apos;Enter the key string: &apos;; cin.getline(key, 25); strcpy(buffer, key); cout &lt;&lt; &apos;Key = &apos;&lt;&lt; key &lt;&lt; endl; cout &lt;&lt; &apos;Buffer = &apos;&lt;&lt; buffer&lt;<endl; return 0; } < pre> <p>Output:</p> <pre> Enter the key string: C++ Tutorial Key = C++ Tutorial Buffer = C++ Tutorial </pre> <hr> <h2>C++ String Length Example</h2> <p>Let&apos;s see the simple example of finding the string length using strlen() function.</p> <pre> #include #include using namespace std; int main() { char ary[] = &apos;Welcome to C++ Programming&apos;; cout &lt;&lt; &apos;Length of String = &apos; &lt;&lt; strlen(ary)&lt;<endl; return 0; } < pre> <p>Output:</p> <pre> Length of String = 26 </pre> <h2>C++ String Functions</h2> <table class="table"> <tr> <th>Function</th> <th>Description</th> </tr> <tr> <td> int compare(const string&amp; str) </td> <td>It is used to compare two string objects.</td> </tr> <tr> <td> <a href="/c-string-length">int length()</a> </td> <td>It is used to find the length of the string.</td> </tr> <tr> <td> void swap(string&amp; str) </td> <td>It is used to swap the values of two string objects.</td> </tr> <tr> <td>string substr(int pos,int n)</td> <td>It creates a new string object of n characters.</td> </tr> <tr> <td> <a href="/c-string-size">int size()</a> </td> <td>It returns the length of the string in terms of bytes.</td> </tr> <tr> <td> void resize(int n) </td> <td>It is used to resize the length of the string up to n characters.</td> </tr> <tr> <td> <a href="/c-string-replace">string&amp; replace(int pos,int len,string&amp; str)</a> </td> <td>It replaces portion of the string that begins at character position pos and spans len characters.</td> </tr> <tr> <td> <a href="/c-string-append">string&amp; append(const string&amp; str)</a> </td> <td>It adds new characters at the end of another string object.</td> </tr> <tr> <td> char&amp; at(int pos) </td> <td>It is used to access an individual character at specified position pos.</td> </tr> <tr> <td> <a href="/c-string-find">int find(string&amp; str,int pos,int n)</a> </td> <td>It is used to find the string specified in the parameter.</td> </tr> <tr> <td> int find_first_of(string&amp; str,int pos,int n) </td> <td>It is used to find the first occurrence of the specified sequence.</td> </tr> <tr> <td> int find_first_not_of(string&amp; str,int pos,int n ) </td> <td>It is used to search the string for the first character that does not match with any of the characters specified in the string.</td> </tr> <tr> <td> int find_last_of(string&amp; str,int pos,int n) </td> <td>It is used to search the string for the last character of specified sequence.</td> </tr> <tr> <td> int find_last_not_of(string&amp; str,int pos) </td> <td>It searches for the last character that does not match with the specified sequence.</td> </tr> <tr> <td> string&amp; insert() </td> <td>It inserts a new character before the character indicated by the position pos.</td> </tr> <tr> <td> int max_size() </td> <td>It finds the maximum length of the string.</td> </tr> <tr> <td> <a href="/c-string-push_back">void push_back(char ch)</a> </td> <td>It adds a new character ch at the end of the string.</td> </tr> <tr> <td> void pop_back() </td> <td>It removes a last character of the string.</td> </tr> <tr> <td> <a href="/c-string-assign">string&amp; assign()</a> </td> <td>It assigns new value to the string.</td> </tr> <tr> <td> int copy(string&amp; str) </td> <td>It copies the contents of string into another.</td> </tr> <tr> <td> char&amp; back() </td> <td>It returns the reference of last character.</td> </tr> <tr> <td> Iterator begin() </td> <td>It returns the reference of first character.</td> </tr> <tr> <td> int capacity() </td> <td>It returns the allocated space for the string.</td> </tr> <tr> <td>const_iterator cbegin()</td> <td>It points to the first element of the string.</td> </tr> <tr> <td>const_iterator cend()</td> <td>It points to the last element of the string.</td> </tr> <tr> <td>void clear()</td> <td>It removes all the elements from the string.</td> </tr> <tr> <td>const_reverse_iterator crbegin()</td> <td>It points to the last character of the string.</td> </tr> <tr> <td>const_char* data()</td> <td>It copies the characters of string into an array.</td> </tr> <tr> <td>bool empty()</td> <td>It checks whether the string is empty or not.</td> </tr> <tr> <td>string&amp; erase()</td> <td>It removes the characters as specified.</td> </tr> <tr> <td> char&amp; front() </td> <td>It returns a reference of the first character.</td> </tr> <tr> <td> string&amp;&#xFFFD; operator+=() </td> <td>It appends a new character at the end of the string.</td> </tr> <tr> <td> string&amp; operator=() </td> <td>It assigns a new value to the string.</td> </tr> <tr> <td>char operator[](pos)</td> <td>It retrieves a character at specified position pos.</td> </tr> <tr> <td> int rfind() </td> <td>It searches for the last occurrence of the string.</td> </tr> <tr> <td> iterator end() </td> <td>It references the last character of the string.</td> </tr> <tr> <td> reverse_iterator rend() </td> <td>It points to the first character of the string.</td> </tr> <tr> <td> void shrink_to_fit() </td> <td>It reduces the capacity and makes it equal to the size of the string.</td> </tr> <tr> <td> char* c_str() </td> <td>It returns pointer to an array that contains null terminated sequence of characters.</td> </tr> <tr> <td> const_reverse_iterator crend() </td> <td>It references the first character of the string.</td> </tr> <tr> <td> reverse_iterator rbegin() </td> <td>It reference the last character of the string.</td> </tr> <tr> <td>void reserve(inr len)</td> <td>It requests a change in capacity.</td> </tr> <tr> <td>allocator_type get_allocator();</td> <td>It returns the allocated object associated with the string.</td> </tr> </table></endl;></pre></endl;></pre></endl;></pre></'answer></pre></s1<<endl;>

Beispiel für einen C++-Stringvergleich

Sehen wir uns das einfache Beispiel eines String-Vergleichs mit der Funktion strcmp() an.

 #include #include using namespace std; int main () { char key[] = &apos;mango&apos;; char buffer[50]; do { cout&lt;&gt;buffer; } while (strcmp (key,buffer) != 0); cout&lt;<\'answer is correct!!\'<<endl; return 0; } < pre> <p>Output:</p> <pre> What is my favourite fruit? apple What is my favourite fruit? banana What is my favourite fruit? mango Answer is correct!! </pre> <hr> <h2>C++ String Concat Example</h2> <p>Let&apos;s see the simple example of string concatenation using strcat() function.</p> <pre> #include #include using namespace std; int main() { char key[25], buffer[25]; cout &lt;&lt; &apos;Enter the key string: &apos;; cin.getline(key, 25); cout &lt;&lt; &apos;Enter the buffer string: &apos;; cin.getline(buffer, 25); strcat(key, buffer); cout &lt;&lt; &apos;Key = &apos; &lt;&lt; key &lt;&lt; endl; cout &lt;&lt; &apos;Buffer = &apos; &lt;&lt; buffer&lt;<endl; return 0; } < pre> <p>Output:</p> <pre> Enter the key string: Welcome to Enter the buffer string: C++ Programming. Key = Welcome to C++ Programming. Buffer = C++ Programming. </pre> <hr> <h2>C++ String Copy Example</h2> <p>Let&apos;s see the simple example of copy the string using strcpy() function.</p> <pre> #include #include using namespace std; int main() { char key[25], buffer[25]; cout &lt;&lt; &apos;Enter the key string: &apos;; cin.getline(key, 25); strcpy(buffer, key); cout &lt;&lt; &apos;Key = &apos;&lt;&lt; key &lt;&lt; endl; cout &lt;&lt; &apos;Buffer = &apos;&lt;&lt; buffer&lt;<endl; return 0; } < pre> <p>Output:</p> <pre> Enter the key string: C++ Tutorial Key = C++ Tutorial Buffer = C++ Tutorial </pre> <hr> <h2>C++ String Length Example</h2> <p>Let&apos;s see the simple example of finding the string length using strlen() function.</p> <pre> #include #include using namespace std; int main() { char ary[] = &apos;Welcome to C++ Programming&apos;; cout &lt;&lt; &apos;Length of String = &apos; &lt;&lt; strlen(ary)&lt;<endl; return 0; } < pre> <p>Output:</p> <pre> Length of String = 26 </pre> <h2>C++ String Functions</h2> <table class="table"> <tr> <th>Function</th> <th>Description</th> </tr> <tr> <td> int compare(const string&amp; str) </td> <td>It is used to compare two string objects.</td> </tr> <tr> <td> <a href="/c-string-length">int length()</a> </td> <td>It is used to find the length of the string.</td> </tr> <tr> <td> void swap(string&amp; str) </td> <td>It is used to swap the values of two string objects.</td> </tr> <tr> <td>string substr(int pos,int n)</td> <td>It creates a new string object of n characters.</td> </tr> <tr> <td> <a href="/c-string-size">int size()</a> </td> <td>It returns the length of the string in terms of bytes.</td> </tr> <tr> <td> void resize(int n) </td> <td>It is used to resize the length of the string up to n characters.</td> </tr> <tr> <td> <a href="/c-string-replace">string&amp; replace(int pos,int len,string&amp; str)</a> </td> <td>It replaces portion of the string that begins at character position pos and spans len characters.</td> </tr> <tr> <td> <a href="/c-string-append">string&amp; append(const string&amp; str)</a> </td> <td>It adds new characters at the end of another string object.</td> </tr> <tr> <td> char&amp; at(int pos) </td> <td>It is used to access an individual character at specified position pos.</td> </tr> <tr> <td> <a href="/c-string-find">int find(string&amp; str,int pos,int n)</a> </td> <td>It is used to find the string specified in the parameter.</td> </tr> <tr> <td> int find_first_of(string&amp; str,int pos,int n) </td> <td>It is used to find the first occurrence of the specified sequence.</td> </tr> <tr> <td> int find_first_not_of(string&amp; str,int pos,int n ) </td> <td>It is used to search the string for the first character that does not match with any of the characters specified in the string.</td> </tr> <tr> <td> int find_last_of(string&amp; str,int pos,int n) </td> <td>It is used to search the string for the last character of specified sequence.</td> </tr> <tr> <td> int find_last_not_of(string&amp; str,int pos) </td> <td>It searches for the last character that does not match with the specified sequence.</td> </tr> <tr> <td> string&amp; insert() </td> <td>It inserts a new character before the character indicated by the position pos.</td> </tr> <tr> <td> int max_size() </td> <td>It finds the maximum length of the string.</td> </tr> <tr> <td> <a href="/c-string-push_back">void push_back(char ch)</a> </td> <td>It adds a new character ch at the end of the string.</td> </tr> <tr> <td> void pop_back() </td> <td>It removes a last character of the string.</td> </tr> <tr> <td> <a href="/c-string-assign">string&amp; assign()</a> </td> <td>It assigns new value to the string.</td> </tr> <tr> <td> int copy(string&amp; str) </td> <td>It copies the contents of string into another.</td> </tr> <tr> <td> char&amp; back() </td> <td>It returns the reference of last character.</td> </tr> <tr> <td> Iterator begin() </td> <td>It returns the reference of first character.</td> </tr> <tr> <td> int capacity() </td> <td>It returns the allocated space for the string.</td> </tr> <tr> <td>const_iterator cbegin()</td> <td>It points to the first element of the string.</td> </tr> <tr> <td>const_iterator cend()</td> <td>It points to the last element of the string.</td> </tr> <tr> <td>void clear()</td> <td>It removes all the elements from the string.</td> </tr> <tr> <td>const_reverse_iterator crbegin()</td> <td>It points to the last character of the string.</td> </tr> <tr> <td>const_char* data()</td> <td>It copies the characters of string into an array.</td> </tr> <tr> <td>bool empty()</td> <td>It checks whether the string is empty or not.</td> </tr> <tr> <td>string&amp; erase()</td> <td>It removes the characters as specified.</td> </tr> <tr> <td> char&amp; front() </td> <td>It returns a reference of the first character.</td> </tr> <tr> <td> string&amp;&#xFFFD; operator+=() </td> <td>It appends a new character at the end of the string.</td> </tr> <tr> <td> string&amp; operator=() </td> <td>It assigns a new value to the string.</td> </tr> <tr> <td>char operator[](pos)</td> <td>It retrieves a character at specified position pos.</td> </tr> <tr> <td> int rfind() </td> <td>It searches for the last occurrence of the string.</td> </tr> <tr> <td> iterator end() </td> <td>It references the last character of the string.</td> </tr> <tr> <td> reverse_iterator rend() </td> <td>It points to the first character of the string.</td> </tr> <tr> <td> void shrink_to_fit() </td> <td>It reduces the capacity and makes it equal to the size of the string.</td> </tr> <tr> <td> char* c_str() </td> <td>It returns pointer to an array that contains null terminated sequence of characters.</td> </tr> <tr> <td> const_reverse_iterator crend() </td> <td>It references the first character of the string.</td> </tr> <tr> <td> reverse_iterator rbegin() </td> <td>It reference the last character of the string.</td> </tr> <tr> <td>void reserve(inr len)</td> <td>It requests a change in capacity.</td> </tr> <tr> <td>allocator_type get_allocator();</td> <td>It returns the allocated object associated with the string.</td> </tr> </table></endl;></pre></endl;></pre></endl;></pre></\'answer>

C++-String-Concat-Beispiel

Sehen wir uns das einfache Beispiel der String-Verkettung mit der Funktion strcat() an.

Methodenteilzeichenfolge Java
 #include #include using namespace std; int main() { char key[25], buffer[25]; cout &lt;&lt; &apos;Enter the key string: &apos;; cin.getline(key, 25); cout &lt;&lt; &apos;Enter the buffer string: &apos;; cin.getline(buffer, 25); strcat(key, buffer); cout &lt;&lt; &apos;Key = &apos; &lt;&lt; key &lt;&lt; endl; cout &lt;&lt; &apos;Buffer = &apos; &lt;&lt; buffer&lt;<endl; return 0; } < pre> <p>Output:</p> <pre> Enter the key string: Welcome to Enter the buffer string: C++ Programming. Key = Welcome to C++ Programming. Buffer = C++ Programming. </pre> <hr> <h2>C++ String Copy Example</h2> <p>Let&apos;s see the simple example of copy the string using strcpy() function.</p> <pre> #include #include using namespace std; int main() { char key[25], buffer[25]; cout &lt;&lt; &apos;Enter the key string: &apos;; cin.getline(key, 25); strcpy(buffer, key); cout &lt;&lt; &apos;Key = &apos;&lt;&lt; key &lt;&lt; endl; cout &lt;&lt; &apos;Buffer = &apos;&lt;&lt; buffer&lt;<endl; return 0; } < pre> <p>Output:</p> <pre> Enter the key string: C++ Tutorial Key = C++ Tutorial Buffer = C++ Tutorial </pre> <hr> <h2>C++ String Length Example</h2> <p>Let&apos;s see the simple example of finding the string length using strlen() function.</p> <pre> #include #include using namespace std; int main() { char ary[] = &apos;Welcome to C++ Programming&apos;; cout &lt;&lt; &apos;Length of String = &apos; &lt;&lt; strlen(ary)&lt;<endl; return 0; } < pre> <p>Output:</p> <pre> Length of String = 26 </pre> <h2>C++ String Functions</h2> <table class="table"> <tr> <th>Function</th> <th>Description</th> </tr> <tr> <td> int compare(const string&amp; str) </td> <td>It is used to compare two string objects.</td> </tr> <tr> <td> <a href="/c-string-length">int length()</a> </td> <td>It is used to find the length of the string.</td> </tr> <tr> <td> void swap(string&amp; str) </td> <td>It is used to swap the values of two string objects.</td> </tr> <tr> <td>string substr(int pos,int n)</td> <td>It creates a new string object of n characters.</td> </tr> <tr> <td> <a href="/c-string-size">int size()</a> </td> <td>It returns the length of the string in terms of bytes.</td> </tr> <tr> <td> void resize(int n) </td> <td>It is used to resize the length of the string up to n characters.</td> </tr> <tr> <td> <a href="/c-string-replace">string&amp; replace(int pos,int len,string&amp; str)</a> </td> <td>It replaces portion of the string that begins at character position pos and spans len characters.</td> </tr> <tr> <td> <a href="/c-string-append">string&amp; append(const string&amp; str)</a> </td> <td>It adds new characters at the end of another string object.</td> </tr> <tr> <td> char&amp; at(int pos) </td> <td>It is used to access an individual character at specified position pos.</td> </tr> <tr> <td> <a href="/c-string-find">int find(string&amp; str,int pos,int n)</a> </td> <td>It is used to find the string specified in the parameter.</td> </tr> <tr> <td> int find_first_of(string&amp; str,int pos,int n) </td> <td>It is used to find the first occurrence of the specified sequence.</td> </tr> <tr> <td> int find_first_not_of(string&amp; str,int pos,int n ) </td> <td>It is used to search the string for the first character that does not match with any of the characters specified in the string.</td> </tr> <tr> <td> int find_last_of(string&amp; str,int pos,int n) </td> <td>It is used to search the string for the last character of specified sequence.</td> </tr> <tr> <td> int find_last_not_of(string&amp; str,int pos) </td> <td>It searches for the last character that does not match with the specified sequence.</td> </tr> <tr> <td> string&amp; insert() </td> <td>It inserts a new character before the character indicated by the position pos.</td> </tr> <tr> <td> int max_size() </td> <td>It finds the maximum length of the string.</td> </tr> <tr> <td> <a href="/c-string-push_back">void push_back(char ch)</a> </td> <td>It adds a new character ch at the end of the string.</td> </tr> <tr> <td> void pop_back() </td> <td>It removes a last character of the string.</td> </tr> <tr> <td> <a href="/c-string-assign">string&amp; assign()</a> </td> <td>It assigns new value to the string.</td> </tr> <tr> <td> int copy(string&amp; str) </td> <td>It copies the contents of string into another.</td> </tr> <tr> <td> char&amp; back() </td> <td>It returns the reference of last character.</td> </tr> <tr> <td> Iterator begin() </td> <td>It returns the reference of first character.</td> </tr> <tr> <td> int capacity() </td> <td>It returns the allocated space for the string.</td> </tr> <tr> <td>const_iterator cbegin()</td> <td>It points to the first element of the string.</td> </tr> <tr> <td>const_iterator cend()</td> <td>It points to the last element of the string.</td> </tr> <tr> <td>void clear()</td> <td>It removes all the elements from the string.</td> </tr> <tr> <td>const_reverse_iterator crbegin()</td> <td>It points to the last character of the string.</td> </tr> <tr> <td>const_char* data()</td> <td>It copies the characters of string into an array.</td> </tr> <tr> <td>bool empty()</td> <td>It checks whether the string is empty or not.</td> </tr> <tr> <td>string&amp; erase()</td> <td>It removes the characters as specified.</td> </tr> <tr> <td> char&amp; front() </td> <td>It returns a reference of the first character.</td> </tr> <tr> <td> string&amp;&#xFFFD; operator+=() </td> <td>It appends a new character at the end of the string.</td> </tr> <tr> <td> string&amp; operator=() </td> <td>It assigns a new value to the string.</td> </tr> <tr> <td>char operator[](pos)</td> <td>It retrieves a character at specified position pos.</td> </tr> <tr> <td> int rfind() </td> <td>It searches for the last occurrence of the string.</td> </tr> <tr> <td> iterator end() </td> <td>It references the last character of the string.</td> </tr> <tr> <td> reverse_iterator rend() </td> <td>It points to the first character of the string.</td> </tr> <tr> <td> void shrink_to_fit() </td> <td>It reduces the capacity and makes it equal to the size of the string.</td> </tr> <tr> <td> char* c_str() </td> <td>It returns pointer to an array that contains null terminated sequence of characters.</td> </tr> <tr> <td> const_reverse_iterator crend() </td> <td>It references the first character of the string.</td> </tr> <tr> <td> reverse_iterator rbegin() </td> <td>It reference the last character of the string.</td> </tr> <tr> <td>void reserve(inr len)</td> <td>It requests a change in capacity.</td> </tr> <tr> <td>allocator_type get_allocator();</td> <td>It returns the allocated object associated with the string.</td> </tr> </table></endl;></pre></endl;></pre></endl;>

Beispiel für das Kopieren von C++-Strings

Sehen wir uns das einfache Beispiel des Kopierens der Zeichenfolge mit der Funktion strcpy() an.

 #include #include using namespace std; int main() { char key[25], buffer[25]; cout &lt;&lt; &apos;Enter the key string: &apos;; cin.getline(key, 25); strcpy(buffer, key); cout &lt;&lt; &apos;Key = &apos;&lt;&lt; key &lt;&lt; endl; cout &lt;&lt; &apos;Buffer = &apos;&lt;&lt; buffer&lt;<endl; return 0; } < pre> <p>Output:</p> <pre> Enter the key string: C++ Tutorial Key = C++ Tutorial Buffer = C++ Tutorial </pre> <hr> <h2>C++ String Length Example</h2> <p>Let&apos;s see the simple example of finding the string length using strlen() function.</p> <pre> #include #include using namespace std; int main() { char ary[] = &apos;Welcome to C++ Programming&apos;; cout &lt;&lt; &apos;Length of String = &apos; &lt;&lt; strlen(ary)&lt;<endl; return 0; } < pre> <p>Output:</p> <pre> Length of String = 26 </pre> <h2>C++ String Functions</h2> <table class="table"> <tr> <th>Function</th> <th>Description</th> </tr> <tr> <td> int compare(const string&amp; str) </td> <td>It is used to compare two string objects.</td> </tr> <tr> <td> <a href="/c-string-length">int length()</a> </td> <td>It is used to find the length of the string.</td> </tr> <tr> <td> void swap(string&amp; str) </td> <td>It is used to swap the values of two string objects.</td> </tr> <tr> <td>string substr(int pos,int n)</td> <td>It creates a new string object of n characters.</td> </tr> <tr> <td> <a href="/c-string-size">int size()</a> </td> <td>It returns the length of the string in terms of bytes.</td> </tr> <tr> <td> void resize(int n) </td> <td>It is used to resize the length of the string up to n characters.</td> </tr> <tr> <td> <a href="/c-string-replace">string&amp; replace(int pos,int len,string&amp; str)</a> </td> <td>It replaces portion of the string that begins at character position pos and spans len characters.</td> </tr> <tr> <td> <a href="/c-string-append">string&amp; append(const string&amp; str)</a> </td> <td>It adds new characters at the end of another string object.</td> </tr> <tr> <td> char&amp; at(int pos) </td> <td>It is used to access an individual character at specified position pos.</td> </tr> <tr> <td> <a href="/c-string-find">int find(string&amp; str,int pos,int n)</a> </td> <td>It is used to find the string specified in the parameter.</td> </tr> <tr> <td> int find_first_of(string&amp; str,int pos,int n) </td> <td>It is used to find the first occurrence of the specified sequence.</td> </tr> <tr> <td> int find_first_not_of(string&amp; str,int pos,int n ) </td> <td>It is used to search the string for the first character that does not match with any of the characters specified in the string.</td> </tr> <tr> <td> int find_last_of(string&amp; str,int pos,int n) </td> <td>It is used to search the string for the last character of specified sequence.</td> </tr> <tr> <td> int find_last_not_of(string&amp; str,int pos) </td> <td>It searches for the last character that does not match with the specified sequence.</td> </tr> <tr> <td> string&amp; insert() </td> <td>It inserts a new character before the character indicated by the position pos.</td> </tr> <tr> <td> int max_size() </td> <td>It finds the maximum length of the string.</td> </tr> <tr> <td> <a href="/c-string-push_back">void push_back(char ch)</a> </td> <td>It adds a new character ch at the end of the string.</td> </tr> <tr> <td> void pop_back() </td> <td>It removes a last character of the string.</td> </tr> <tr> <td> <a href="/c-string-assign">string&amp; assign()</a> </td> <td>It assigns new value to the string.</td> </tr> <tr> <td> int copy(string&amp; str) </td> <td>It copies the contents of string into another.</td> </tr> <tr> <td> char&amp; back() </td> <td>It returns the reference of last character.</td> </tr> <tr> <td> Iterator begin() </td> <td>It returns the reference of first character.</td> </tr> <tr> <td> int capacity() </td> <td>It returns the allocated space for the string.</td> </tr> <tr> <td>const_iterator cbegin()</td> <td>It points to the first element of the string.</td> </tr> <tr> <td>const_iterator cend()</td> <td>It points to the last element of the string.</td> </tr> <tr> <td>void clear()</td> <td>It removes all the elements from the string.</td> </tr> <tr> <td>const_reverse_iterator crbegin()</td> <td>It points to the last character of the string.</td> </tr> <tr> <td>const_char* data()</td> <td>It copies the characters of string into an array.</td> </tr> <tr> <td>bool empty()</td> <td>It checks whether the string is empty or not.</td> </tr> <tr> <td>string&amp; erase()</td> <td>It removes the characters as specified.</td> </tr> <tr> <td> char&amp; front() </td> <td>It returns a reference of the first character.</td> </tr> <tr> <td> string&amp;&#xFFFD; operator+=() </td> <td>It appends a new character at the end of the string.</td> </tr> <tr> <td> string&amp; operator=() </td> <td>It assigns a new value to the string.</td> </tr> <tr> <td>char operator[](pos)</td> <td>It retrieves a character at specified position pos.</td> </tr> <tr> <td> int rfind() </td> <td>It searches for the last occurrence of the string.</td> </tr> <tr> <td> iterator end() </td> <td>It references the last character of the string.</td> </tr> <tr> <td> reverse_iterator rend() </td> <td>It points to the first character of the string.</td> </tr> <tr> <td> void shrink_to_fit() </td> <td>It reduces the capacity and makes it equal to the size of the string.</td> </tr> <tr> <td> char* c_str() </td> <td>It returns pointer to an array that contains null terminated sequence of characters.</td> </tr> <tr> <td> const_reverse_iterator crend() </td> <td>It references the first character of the string.</td> </tr> <tr> <td> reverse_iterator rbegin() </td> <td>It reference the last character of the string.</td> </tr> <tr> <td>void reserve(inr len)</td> <td>It requests a change in capacity.</td> </tr> <tr> <td>allocator_type get_allocator();</td> <td>It returns the allocated object associated with the string.</td> </tr> </table></endl;></pre></endl;>

Beispiel für die C++-Stringlänge

Sehen wir uns das einfache Beispiel zum Ermitteln der Stringlänge mit der Funktion strlen() an.

 #include #include using namespace std; int main() { char ary[] = &apos;Welcome to C++ Programming&apos;; cout &lt;&lt; &apos;Length of String = &apos; &lt;&lt; strlen(ary)&lt;<endl; return 0; } < pre> <p>Output:</p> <pre> Length of String = 26 </pre> <h2>C++ String Functions</h2> <table class="table"> <tr> <th>Function</th> <th>Description</th> </tr> <tr> <td> int compare(const string&amp; str) </td> <td>It is used to compare two string objects.</td> </tr> <tr> <td> <a href="/c-string-length">int length()</a> </td> <td>It is used to find the length of the string.</td> </tr> <tr> <td> void swap(string&amp; str) </td> <td>It is used to swap the values of two string objects.</td> </tr> <tr> <td>string substr(int pos,int n)</td> <td>It creates a new string object of n characters.</td> </tr> <tr> <td> <a href="/c-string-size">int size()</a> </td> <td>It returns the length of the string in terms of bytes.</td> </tr> <tr> <td> void resize(int n) </td> <td>It is used to resize the length of the string up to n characters.</td> </tr> <tr> <td> <a href="/c-string-replace">string&amp; replace(int pos,int len,string&amp; str)</a> </td> <td>It replaces portion of the string that begins at character position pos and spans len characters.</td> </tr> <tr> <td> <a href="/c-string-append">string&amp; append(const string&amp; str)</a> </td> <td>It adds new characters at the end of another string object.</td> </tr> <tr> <td> char&amp; at(int pos) </td> <td>It is used to access an individual character at specified position pos.</td> </tr> <tr> <td> <a href="/c-string-find">int find(string&amp; str,int pos,int n)</a> </td> <td>It is used to find the string specified in the parameter.</td> </tr> <tr> <td> int find_first_of(string&amp; str,int pos,int n) </td> <td>It is used to find the first occurrence of the specified sequence.</td> </tr> <tr> <td> int find_first_not_of(string&amp; str,int pos,int n ) </td> <td>It is used to search the string for the first character that does not match with any of the characters specified in the string.</td> </tr> <tr> <td> int find_last_of(string&amp; str,int pos,int n) </td> <td>It is used to search the string for the last character of specified sequence.</td> </tr> <tr> <td> int find_last_not_of(string&amp; str,int pos) </td> <td>It searches for the last character that does not match with the specified sequence.</td> </tr> <tr> <td> string&amp; insert() </td> <td>It inserts a new character before the character indicated by the position pos.</td> </tr> <tr> <td> int max_size() </td> <td>It finds the maximum length of the string.</td> </tr> <tr> <td> <a href="/c-string-push_back">void push_back(char ch)</a> </td> <td>It adds a new character ch at the end of the string.</td> </tr> <tr> <td> void pop_back() </td> <td>It removes a last character of the string.</td> </tr> <tr> <td> <a href="/c-string-assign">string&amp; assign()</a> </td> <td>It assigns new value to the string.</td> </tr> <tr> <td> int copy(string&amp; str) </td> <td>It copies the contents of string into another.</td> </tr> <tr> <td> char&amp; back() </td> <td>It returns the reference of last character.</td> </tr> <tr> <td> Iterator begin() </td> <td>It returns the reference of first character.</td> </tr> <tr> <td> int capacity() </td> <td>It returns the allocated space for the string.</td> </tr> <tr> <td>const_iterator cbegin()</td> <td>It points to the first element of the string.</td> </tr> <tr> <td>const_iterator cend()</td> <td>It points to the last element of the string.</td> </tr> <tr> <td>void clear()</td> <td>It removes all the elements from the string.</td> </tr> <tr> <td>const_reverse_iterator crbegin()</td> <td>It points to the last character of the string.</td> </tr> <tr> <td>const_char* data()</td> <td>It copies the characters of string into an array.</td> </tr> <tr> <td>bool empty()</td> <td>It checks whether the string is empty or not.</td> </tr> <tr> <td>string&amp; erase()</td> <td>It removes the characters as specified.</td> </tr> <tr> <td> char&amp; front() </td> <td>It returns a reference of the first character.</td> </tr> <tr> <td> string&amp;&#xFFFD; operator+=() </td> <td>It appends a new character at the end of the string.</td> </tr> <tr> <td> string&amp; operator=() </td> <td>It assigns a new value to the string.</td> </tr> <tr> <td>char operator[](pos)</td> <td>It retrieves a character at specified position pos.</td> </tr> <tr> <td> int rfind() </td> <td>It searches for the last occurrence of the string.</td> </tr> <tr> <td> iterator end() </td> <td>It references the last character of the string.</td> </tr> <tr> <td> reverse_iterator rend() </td> <td>It points to the first character of the string.</td> </tr> <tr> <td> void shrink_to_fit() </td> <td>It reduces the capacity and makes it equal to the size of the string.</td> </tr> <tr> <td> char* c_str() </td> <td>It returns pointer to an array that contains null terminated sequence of characters.</td> </tr> <tr> <td> const_reverse_iterator crend() </td> <td>It references the first character of the string.</td> </tr> <tr> <td> reverse_iterator rbegin() </td> <td>It reference the last character of the string.</td> </tr> <tr> <td>void reserve(inr len)</td> <td>It requests a change in capacity.</td> </tr> <tr> <td>allocator_type get_allocator();</td> <td>It returns the allocated object associated with the string.</td> </tr> </table></endl;>

C++-String-Funktionen

Funktion Beschreibung
int vergleichen(const string& str) Es wird verwendet, um zwei String-Objekte zu vergleichen.
int Länge() Es wird verwendet, um die Länge der Zeichenfolge zu ermitteln.
void swap(string& str) Es wird verwendet, um die Werte zweier String-Objekte auszutauschen.
string substr(int pos,int n) Es erstellt ein neues String-Objekt mit n Zeichen.
int size() Es gibt die Länge der Zeichenfolge in Bytes zurück.
void resize(int n) Es wird verwendet, um die Länge der Zeichenfolge auf bis zu n Zeichen zu ändern.
string& replace(int pos,int len,string& str) Es ersetzt einen Teil der Zeichenfolge, der an der Zeichenposition pos beginnt und sich über len Zeichen erstreckt.
string& append(const string& str) Es fügt neue Zeichen am Ende eines anderen String-Objekts hinzu.
char& at(int pos) Es wird verwendet, um auf ein einzelnes Zeichen an der angegebenen Position pos zuzugreifen.
int find(string& str,int pos,int n) Es wird verwendet, um die im Parameter angegebene Zeichenfolge zu finden.
int find_first_of(string& str,int pos,int n) Es wird verwendet, um das erste Vorkommen der angegebenen Sequenz zu finden.
int find_first_not_of(string& str,int pos,int n ) Es wird verwendet, um die Zeichenfolge nach dem ersten Zeichen zu durchsuchen, das mit keinem der in der Zeichenfolge angegebenen Zeichen übereinstimmt.
int find_last_of(string& str,int pos,int n) Es wird verwendet, um die Zeichenfolge nach dem letzten Zeichen der angegebenen Sequenz zu durchsuchen.
int find_last_not_of(string& str,int pos) Es wird nach dem letzten Zeichen gesucht, das nicht mit der angegebenen Reihenfolge übereinstimmt.
string& insert() Es fügt ein neues Zeichen vor dem durch die Position pos angegebenen Zeichen ein.
int max_size() Es ermittelt die maximale Länge der Zeichenfolge.
void push_back(char ch) Es fügt am Ende der Zeichenfolge ein neues Zeichen ch hinzu.
void pop_back() Es entfernt ein letztes Zeichen der Zeichenfolge.
string&zuordnen() Es weist der Zeichenfolge einen neuen Wert zu.
int copy(string& str) Es kopiert den Inhalt einer Zeichenfolge in eine andere.
char& back() Es gibt die Referenz des letzten Zeichens zurück.
Iterator begin() Es gibt die Referenz des ersten Zeichens zurück.
int Kapazität() Es gibt den zugewiesenen Speicherplatz für die Zeichenfolge zurück.
const_iterator cbegin() Es zeigt auf das erste Element der Zeichenfolge.
const_iterator cend() Es zeigt auf das letzte Element der Zeichenfolge.
void klar() Es entfernt alle Elemente aus der Zeichenfolge.
const_reverse_iterator crbegin() Es zeigt auf das letzte Zeichen der Zeichenfolge.
const_char* data() Es kopiert die Zeichen einer Zeichenfolge in ein Array.
bool leer() Es prüft, ob die Zeichenfolge leer ist oder nicht.
string& erase() Es entfernt die Zeichen wie angegeben.
char& front() Es gibt eine Referenz des ersten Zeichens zurück.
string&� Operator+=() Es fügt am Ende der Zeichenfolge ein neues Zeichen hinzu.
string& Operator=() Es weist der Zeichenfolge einen neuen Wert zu.
char-Operator[](pos) Es ruft ein Zeichen an der angegebenen Position pos ab.
int rfind() Es wird nach dem letzten Vorkommen der Zeichenfolge gesucht.
Iterator end() Es verweist auf das letzte Zeichen der Zeichenfolge.
reverse_iterator rend() Es zeigt auf das erste Zeichen der Zeichenfolge.
void Shrink_to_fit() Es reduziert die Kapazität und gleicht sie der Größe der Saite an.
char* c_str() Es gibt einen Zeiger auf ein Array zurück, das eine mit Null abgeschlossene Zeichenfolge enthält.
const_reverse_iterator crend() Es verweist auf das erste Zeichen der Zeichenfolge.
reverse_iterator rbegin() Es verweist auf das letzte Zeichen der Zeichenfolge.
Leerguthaben(inr len) Es beantragt eine Kapazitätsänderung.
allocator_type get_allocator(); Es gibt das zugeordnete Objekt zurück, das der Zeichenfolge zugeordnet ist.