Summary of usage of string class objects in C + +

Source: Internet
Author: User


C + + In contrast to the C language, greatly enhanced support for strings, in addition to the use of C-style strings, you can also use the built-in data type string,string class processing string because the function of the encapsulation is particularly convenient, let us count the function of the string class



First, you want to call the string class to include its header file first#include <string>


String s1; / / variable s1 is only defined but not initialized, so its default value is "" empty string string s2="Hello World!"; 
/ / variable s2 is defined at the same time is initialized / * string class variable can Direct assignment to each other, do not need to use the strcpy () function to assign a character to a character * 
/ / / such as string s3 = s2; / / s3 content is the same as s2 Hello World! / If you need to define a string consisting of many of the same characters, there is another simple way to write string s4(int n,char c); 
///4 is a string initialized to the character c of n


About the length of the string, in C, we can use the strlen () function, C + + can also use strlen (S3), this method to find the actual length of the S3 string, but because C + + about the string class and C language has an essential difference, So we generally call the String.Length () function to find the length of the string


int Len=0;len=string.length (S3);cout<< "S3 string length is" <<len<<endl;


As we mentioned above, if you assign one string string to another string, you just have to assign the value directly, but what if the string class is assigned to the Char* class or the Char* class to the String class? Of course, it can't be directly assigned, just look at the code.


/ / String class assignment to the string class string s1 = "hello world"; string s2;

S2=s1;//string class assignment to char* class string s1="hello world";char str[20]={0};

Strcpy_s(str,s1.c_str());//char* class assignment to the string class char str[20]="hello world";string s2;

S2=str;


Also, a variable of type string can use the manipulation of a character array to change one of its variables, for example


#include <iostream>#include <string>string s1="this is my house";int i;
//If we want to change one of the characters now, we can directly treat s1 as an array and find the corresponding subscript to change i =6;

s[i]='t';//This will change the 6th character to t



With the string class, we can use the "+" or "+ =" operator to directly stitch strings, it is very convenient, no longer need to use C language strcat (), strcpy (), malloc () and other functions to splice strings, no longer worry about the space will overflow, with "+" To stitch a string, either side of the operator can be a string string, a string string and a C-style string, or a string string and a char character.



Assignment of the String class


String &operator=(const string &s);//Assign the string s to the current string string &assign(const char *s);//Use the c type string s to assign string &assign(const char *s,int n); / / Use the c string s start n characters assigned string &assign (const string & s); / / assign the string s to the current string
String &assign(int n,char c);//Use n characters c to assign the current string to string &assign(const string &s,int start,int n);//put n characters from the start of the string s Assign to the current string
String &assign(const_iterator first,const_itertor last);//Assign the part between the first and last iterators to the string


Connection to String


String &operator+= (const string &s);//Connect the string s to the end of the current string, 

string&append (const char *s);// Concatenate the C-type string s to the end of the current string, 

string &append (const char *s,int n), or the first n characters of type C string s connect prompt to the end of the current string, string &append (const string &s);   

 Same as operator+= () string &append (const string &s,int pos,int N);//n characters starting from Pos in string s connect prompt to the end of the current string & Append (int n,char c);       

 Adds n characters to the end of the current string CString &append (Const_iterator first,const_iterator last);//Connect the part between the iterator first and last to the end of the current string


Substring of stringstring substr(int pos = 0,int n = npos) const;//Returns a string of n characters starting with pos


void Swap (string &s2);    Swap the current string with the value of S2


Search for string



RFind () and find () are similar, looking for substrings in a string, but the find () function looks for the second argument, and the RFind () function finds at most the second argument, and if the subscript specified by the second parameter has not found a substring, Returns an infinity value of 4294967295


int find (char c, int pos = 0) const;//from the Pos to find the character C at the position of the current

 string int find (const char *s, int pos = 0) const;//from the Pos to find the string s bit in the current string Set 

int find (const char *s, int pos, int n) const;//from Pos to find the position of the first n characters in the string s in the current string, 

int find (const string &s, int pos = 0) Cons t;//starting from Pos to find the position of the string s in the current string 

int rfind (char c, int pos = NPOs) const;//from the Pos starting from the back forward looking for character C in the current string position 

int rfind (const char *s, int pos = NPOs) const;int rfind (const char *s, int pos, int n = npos) const;

int rfind (const string &s,int pos = NPOs) const;// Finds the position of the first n characters in the string s in the current string from the beginning of the POS, and returns the value of String::npos on failure.



Insert function for String class:


String &insert (int p0, const char *s), string &insert (int p0, const char *s, int n); 

string &insert (int p0,const String &s);

 string &insert (int p0,const string &s, int pos, int n),///First 4 functions Insert string S in p0 position the first n characters of Pos starting AT & string Insert (int p0, int n, char c);

//This function inserts n characters at P0 citerator Insert (iterator it, char c);

//Inserts the character C in it, returns the position of the post-insertion iterator void insert ( Iterator it, Const_iterator first, const_iterator last);

//Insert the character void insert (iterator it, int n, char c) between [First,last] in the IT branch; Insert n characters c in it



The use of the String class object in C + + is a bit of a foundation today.


Related Article

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.