In practice, the string series in c ++ -- string replacement and search (path-related operations)

Source: Internet
Author: User

In practice, the string series in c ++ -- string replacement and search (path-related operations)

Continue to write some string operations today.
String provides us with a lot of methods, but every time you use it, it will take some twists and turns.

Scenario 1:
Get a std: string full_path = "D: program filescsdn", but I want to get the path "D: program filesagaa.
This requires replacement of strings.

std::string full_path = D:\program files\csdnconst size_t last_slash_idx1 = full_path .find_last_of(\/);if (std::string::npos != last_slash_idx1){    full_path.erase(last_slash_idx1, std::string::npos);}std::string my_path = full_path + \vagaa;

Here the string* Find_last_of *Method, andEraseMethod.

Scenario 2:
Get a std: string full_image_path = "D: program filescsdnagaa.png", but I want to get the file extension based on this path.
We can use the find_last_of + erase method. We can also do this:

std::string full_image_path = D:\program files\csdn\vagaa.pngstd::string file_extension_name;size_t i = full_image_path .rfind('.', file_path.length());if (i != string::npos) {    file_extension_name = full_image_path .substr(i + 1, full_image_path .length() - i);}

In this case, the stringRfindMethod andSubstrMethod

Scenario 3

 

Constructor of the string class:
String (const char * s); // use the c string s for initialization
String (int n, char c); // initialize with n characters c
In addition, the string class supports the default constructor and the copy constructor, such as string s1; string s2 = "hello. If the constructed string is too long to be expressed, the length_error exception is thrown;

String character operations:
Const char & operator [] (int n) const;
Const char & at (int n) const;
Char & operator [] (int n );
Char & at (int n );
Operator [] and at () both return the position of the nth character in the current string. However, the at function provides a range check and throws an out_of_range exception when the current string is out of bounds, the subscript operator [] does not provide access check.
Const char * data () const; // returns a non-null ending c character array
Const char * c_str () const; // returns a null-terminated c string.
Int copy (char * s, int n, int pos = 0) const; // copy the n characters starting with pos in the current string to the character array starting with s, and return the actual number of copies

Feature description of string:
Int capacity () const; // returns the current capacity (that is, the number of elements in the string that can be stored without adding memory)
Int max_size () const; // returns the maximum length of a string that can be stored in a string object.
Int size () const; // returns the size of the current string
Int length () const; // returns the length of the current string
Bool empty () const; // whether the current string is null
Void resize (int len, char c); // set the current size of the string to len and fill the missing part with character c.

Input and Output operations of the string class:
String type heavy-duty operator> used for input, also heavy-duty operator <used for output operations.
The getline (istream & in, string & s) function is used to read strings from the input stream in to s and separate them with line breaks.

String Value assignment:
String & operator = (const string & s); // assign string s to the current string
String & assign (const char * s); // use the c-type string s to assign a value
String & assign (const char * s, int n); // assign values with n characters starting with string s
String & assign (const string & s); // assign string s to the current string
String & assign (int n, char c); // assign a value to the current string with n characters c
String & assign (const string & s, int start, int n); // assign the n characters starting from start in string s to the current string
String & assign (const_iterator first, const_itertor last); // assign the part between the first and last iterators to the string

String connection:
String & operator ++ = (const string & s); // connects string s to the end of the current string
String & append (const char * s); // connect string s of the c type to the end of the current string
String & append (const char * s, int n); // connect the first n characters of the c-type string s to the end of the current string
String & append (const string & s); // same as operator + = ()
String & append (const string & s, int pos, int n); // The n characters starting from pos in the string are connected to the end of the current string
String & append (int n, char c); // Add n characters to the end of the current string
String & append (const_iterator first, const_iterator last); // link the part of the iterator first and last to the end of the current string

String comparison:
Bool operator = (const string & s1, const string & s2) const; // compare whether two strings are equal
Operator ">", "<", "> =", "<= ","! = "Are reloaded for string comparison;
Int compare (const string & s) const; // compare the size of the current string and s
Int compare (int pos, int n, const string & s) const; // compare the size of s and the string consisting of n characters starting from pos of the current string
Int compare (int pos, int n, const string & s, int pos2, int n2) const;
Int compare (const char * s) const;
Int compare (int pos, int n, const char * s) const;
Int compare (int pos, int n, const char * s, int pos2) const;
When the compare function is>, 1 is returned. If the value is <,-1 is returned. If the value is =, 0 is returned.

Substring of string:
String substr (int pos = 0, int n = npos) const; // returns a string consisting of n characters starting with pos.

String exchange:
Void swap (string & s2); // exchange the value of the current string and s2

String-type lookup functions:
Int find (char c, int pos = 0) const; // start from pos to find the position of character c in the current string
Int find (const char * s, int pos = 0) const; // start from pos to find the position of string s in the current string
Int find (const char * s, int pos, int n) const; // start from pos to find the position of the First n characters in string s in the current string
Int find (const string & s, int pos = 0) const; // search for the position of string s in the current string from pos
// Return the location when the search is successful. If the search fails, return the string: npos value.
Int rfind (char c, int pos = npos) const; // search for the position of character c in the current string from the pos to the forward
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;
// Start from the pos and start from the back to the front to find the position of the string consisting of the First n characters in string s in the current string. If the string fails, return the value of string: npos.
Int find_first_of (char c, int pos = 0) const; // search for the position where character c appears for the first time from pos
Int find_first_of (const char * s, int pos = 0) const;
Int find_first_of (const char * s, int pos, int n) const;
Int find_first_of (const string & s, int pos = 0) const;
// Start from pos to find the position of the first character in the array consisting of the First n characters of s in the current string. String: npos
Int find_first_not_of (char c, int pos = 0) const;
Int find_first_not_of (const char * s, int pos = 0) const;
Int find_first_not_of (const char * s, int pos, int n) const;
Int find_first_not_of (const string & s, int pos = 0) const;
// Find the position of the first character not in the string s from the current string. If the string fails to appear, string: npos is returned.
Int find_last_of (char c, int pos = npos) const;
Int find_last_of (const char * s, int pos = npos) const;
Int find_last_of (const char * s, int pos, int n = npos) const;
Int find_last_of (const string & s, int pos = npos) const;
Int find_last_not_of (char c, int pos = npos) const;
Int find_last_not_of (const char * s, int pos = npos) const;
Int find_last_not_of (const char * s, int pos, int n) const;
Int find_last_not_of (const string & s, int pos = npos) const;
// Find_last_of and find_last_not_of are similar to find_first_of and find_first_not_of, but they are searched forward from the back.

Replacement functions of the string class:
String & replace (int p0, int n0, const char * s); // Delete the n0 characters starting with p0, and insert the string s at p0
String & replace (int p0, int n0, const char * s, int n); // Delete n0 characters starting with p0, insert the first n characters of string s in p0.
String & replace (int p0, int n0, const string & s); // Delete the n0 characters starting with p0, and insert the string s at p0
String & replace (int p0, int n0, const string & s, int pos, int n); // Delete n0 characters starting with p0, then insert n characters starting from pos in string s at p0
String & replace (int p0, int n0, int n, char c); // Delete n0 characters starting with p0, and then insert n characters c at p0
String & replace (iterator first0, iterator last0, const char * s); // replace the parts between [first0, last0) with the string s
String & replace (iterator first0, iterator last0, const char * s, int n); // replace the portion between [first0, last0) with the first n characters of s
String & replace (iterator first0, iterator last0, const string & s); // replace the parts between [first0, last0) with the string s
String & replace (iterator first0, iterator last0, int n, char c); // replace the part between [first0, last0) with n characters c
String & replace (iterator first0, iterator last0, const_iterator first, const_iterator last); // replace the parts between [first0, last0) with strings between [first, last)

Insert functions of the 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 );
// The first four functions Insert the first n characters starting with pos in the string s at the p0 position
String & insert (int p0, int n, char c); // This function inserts n characters in p0 c
Iterator insert (iterator it, char c); // insert character c in it to return the position of the iterator after insertion
Void insert (iterator it, const_iterator first, const_iterator last); // insert characters between [first, last) in it
Void insert (iterator it, int n, char c); // insert n characters in it

Delete functions of the string class
Iterator erase (iterator first, iterator last); // deletes all characters between [first, last) and returns the position of the iterator after deletion.
Iterator erase (iterator it); // deletes the characters it points to and returns the position of the iterator after deletion.
String & erase (int pos = 0, int n = npos); // Delete the n characters starting with pos and return the modified string

String type iterator processing:
The string class provides the iterator for Traversing forward and backward. The iterator provides the syntax for accessing each character, similar to pointer operations. The iterator does not check the range.
Use string: iterator or string: const_iterator to declare the iterator variable. const_iterator cannot change the content of the iteration. Common iterator functions include:
Const_iterator begin () const;
Iterator begin (); // returns the starting position of the string.
Const_iterator end () const;
Iterator end (); // returns the position after the last character of string
Const_iterator rbegin () const;
Iterator rbegin (); // returns the position of the last character of string
Const_iterator rend () const;
Iterator rend (); // returns the first character of the string.
Rbegin and rend are used for iterative access from the back and forward. They are implemented by setting the iterator string: reverse_iterator, string: const_reverse_iterator

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.