C + + STL string

Source: Internet
Author: User

Initialization
Function prototypes Function
String () To create an empty string
String(const string& str) Initializes another string object with one string object
String(const Char* s) Class with C-style string s
string (int n, char c) class with n characters c.

Assign Value
Function prototypes Function
String& operator= (const Char* s) char* type string assignment to the current string
string& operator= (const string &s) Assigns the C-style string s to the current string
String& operator= (char c) The character assigned value to the current string
String& Assign (const char *s) assigns the string s to the current string
String& Assign (const char *s, int n) Assigned the first n characters of the string s to the current string
String& Assign (const string &s) Assigns the string s to the current string
String& Assign (int n, char c) assigns the current string with n characters c
String& Assign (const string &s, int start, int n) start s n characters assigned value from Start to string

Access character
Function prototypes Function
Char& operator[] (int n) Use [] method to take character, no subscript check
Char& at (int n) Using the at method to obtain the character, there will be subscript legality check

Stitching
Function prototypes Function
String& operator+ = (const string& str) Overloaded + = operator
String& operator+ = (const char* str) Overloaded + = operator
String& operator+ = (const char c) Overloaded + = operator
String& Append (const char *s) Concatenate the string s to the end of the current string
String& Append (const char *s, int n) Connect prompt the first n characters of the string s to the end of the current string
String& Append (const string &s) With operator+= ()
String& Append (const string &s, int pos, int n) Connect prompt the n characters from the POS in the string s to the end of the current string
String& Append (int n, char c) Adds n characters to the end of the current string C

Find and replace
Function prototypes Function
int Find (const string& str, int pos = 0) const Find the first occurrence of Str , start from POS
int Find (const Char* s, int pos = 0) const Find The first occurrence of S , start from POS
int Find (const Char* s, int pos, int n) const /c4> Find The first n characters of S from the pos position position
int Find (const char c, int pos = 0) const /c16> Find character c first occurrence position
int rfind (const string& str, int pos = NPOs) const Find the last location of str and find it from POS
int rfind (const Char* s, int pos = NPOs) const Find the last occurrence of S , starting from Pos
int rfind (const Char* s, int pos, int n) const Find the first n characters of S from Pos last position
int rfind (const char c, int pos = 0) const Find character c last occurrence position
String& Replace (int pos, int n, const string& str) Replace n characters from Pos starting with a string str
String& Replace (int pos, int n, const Char* s) Replace n characters starting with Pos As String s

Comparison
Function prototypes Function
int Compare (const string &s) const Compare to string s
int Compare (const char *s) const Compare to string s

Sub-string
Function prototypes Function
String substr (int pos = 0, int n = npos) const Returns a string of n characters starting with the POS

inserting and deleting
Function prototypes Function
String& Insert (int pos, const Char* s) Insert C-style string s before POS location
String& Insert (int pos, const string& str) Insert string in front of POS location str
String& Insert (int pos, int N, char c) Insert n characters before POS position C
String& Erase (int pos, int n = npos) Delete n characters starting at POS location

C + + STL string

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.