The most complete summary of common operation functions of C++ strings

[Strings] are often used in the process of doing questions , and sometimes it takes a lot of effort to write a function, and finally found that C++ has already encapsulated such functions. . . So take a moment to summarize, here is the reference:

  1. Constructor:
    string str: empty string, string s(str) copies str to s, and string s(num,c) generates a string composed of num c characters.

  2. Insert function:
    There are two kinds of push_back() and insert(), the latter is more commonly used.
    push_back(): insert a character at the end , such as s.push_back(‘p’);
    insert(): such as s=”abcddss”, execute s.insert(s.begin(),’i’) before position 0 Inserting ‘i’ becomes “iabcddss”.

  3. Traversal:
    With the help of iterators or subscripts, subscripts are more commonly used. There are two types of iterators: forward and reverse.
    Forward: string::iterator it=s.begin();it!=s.end();it++;
    Reverse: string::reverse_iterator it=s.rebegin();it!=s.rend(); it++;
    where *it represents the character at that position. Subscripts are the same as character arrays and will not be repeated here.

  4. Find:
    1) Find a string: s.find(ss), find and return the starting position of ss in s, otherwise return -1;
    2) Find a character from a certain position: s. find(‘t’,6), search for the character ‘t’ from position 6 of s, return the position if found, otherwise return -1; note that 1) and 2) both return the position found for the first time;
    3), Find a character from the end: s.rfind(‘t’);
    4) Start from a certain position to find the first character position that does not belong to s1, such as s=”cdcdscds”, s1=”cdcdfff”, s .find_first_not_of(s1)=4, the 4th character in the s position does not belong to s1, and similarly there is a position to find the first character belonging to s1, just remove the not.

  5. Sort:

  6. Segmentation and interception:
    There are two functions: substr() and strtok().
    s.substr(2,5) is a 5-character substring of s starting at position 2.
    strtok() function prototype: char strtok(char s,const char *delim)
    function function: decompose a string into a set of strings, s is the string to be decomposed, and delim is a delimited string.

const char *split=",;!";
char *p=strtok(str,split); 
 while(p!=NULL) {   
    p=strtok( NULL , split);   

 // Split the string str return  0 according to the delimiter ",:!"  

  1. Delete:
    erase() function, such as s=“12345678”, s.erase(s.begin()+3) becomes “1235678”, that is, the character in position 3 is deleted.

  2. Replace:
    repalce() function: s.replace(pos,len,ss), replace the len characters starting from pos with ss,
    s.replace(pos,n1,n2,c), replace s from pos The first n1 characters are replaced with n2 characters c .

  3. Case conversion:
    tolower() and toupper(). Loop through each position of the string s[i]=tolower(s[i]), uppercase to lowercase, or s[i]=toupper(s[i]), lowercase to uppercase.
    In fact, it is recommended to use the transform() function in STL. The specific usage is as follows:
    transform(s.begin(), s.end(), s.begin(), ::tolower) uppercase to lowercase, and lowercase to uppercase is similar.

  4. Comparison function:
    string supports <,>,<=, >=, ==, != and other operations.
    The compare() function is specially used as a comparison function for strings. means ss == s, returning 1 means s>ss, and returning -1 means s<ss.

  5. Splicing function:
    C++ string supports direct addition, “cdd” + “sss” = “cddsss”. In addition, s.append(ss) means adding ss at the end of s, which is equivalent to s+=ss.

Leave a Comment

Your email address will not be published. Required fields are marked *