C/C++ commonly used string manipulation

1, string to plastic

int atoi(const char *str );

Note: input is a C-style character array, if it is a C++ string type, the input can call str.c_str () to input C-style characters String;

If the input is not all numeric type characters, the conversion will stop at the first character that is not a numeric type.

example:

string str = "181.484";
int x = atoi(str.c_str());

In the above example, x is equal to 181;

2, substring

string str = "giregjniuer";
String sub1 = str.substr(1); // take the subscript 1 of str to the last position as a substring
String sub1 = str.substr(0,5); //The 5 characters after the start of the subscript 0 of str are the substring 

3, and the string is searched from the string

(1) Size_t find (const string& str, size_t pos = 0) const; // lookup object--string class object

(2)size_t find (const char* s, size_t pos = 0) const; //find object- - String

(3)size_t find (const char* s, size_t pos, size_t n) const; //Find the object--the first n characters of the string

(4)size_t find (char c, size_t pos = 0) const; // lookup object--character

result: find -- return index of the first character

not found -- return string::npos

string Str = "qau ni ma b";
Int id1 = str.find("ni");
Int id2 = str.find("a",id1); // Look backwards from the id1 position. The found subscript is relative to the entire string instead of id1.
Int id3 = str.find("ni m",0,2); //The third parameter indicates the first few characters of the string (the first parameter) to be looked up 

4, cutting the string The

cut string can be implemented by find and substr, or by the following function.

char* strtok (char* str,constchar* delimiters );

function function:Cut the string, split str into a substring function parameter:str: at the first time called str is the string that needs to be cut The first address; NULL is passed in the time that is called later. Delimiters: Indicates the cut string (each character in the string is treated as a separator). function return value: When the character in s finds the end, it returns NULL; If the character indicated by delimiter is not found, a pointer to the current strtok string is returned.

char buf[]="[email protected]@[email protected]@heima";
    char*temp = strtok(buf,"@");
    while(temp)
    {
        printf("%s ",temp);
        temp = strtok(NULL,"@");
    }

5, replace string

replace (replace)

语法:

  basic_string &replace( size_type index, size_type num, const basic_string &str );
  basic_string &replace( size_type index1, size_type num1, const basic_string &str, size_type index2,
  size_type num2 );
  basic_string &replace( size_type index, size_type num, const char *str );
  basic_string &replace( size_type index, size_type num1, const char *str, size_type num2 );
  basic_string &replace( size_type index, size_type num1, size_type num2, char ch );
  basic_string &replace( iterator start, iterator end, const basic_string &str );
  basic_string &replace( iterator start, iterator end, const char *str );
  basic_string &replace( iterator start, iterator end, const char *str, size_type num );
  basic_string &replace( iterator start, iterator end, size_type num, char ch );

replace() function:

  • Replace the characters in this string with num characters in str Start with index
  • Replace the characters in this string with num2 characters in str (starting from index2), starting with index1, up to num1 characters
  • with num characters in str (from index Start) Replace the character in this string
  • Replace the character in this string with num2 characters in str (starting from index2), starting with index1, replacing num1 characters
  • with num2 ch characters The characters in this string, starting from index
  • replace the characters in this string with the characters in str, the iterator start and end indication range
  • replace the string with num characters in str The content, iterator start and end indication range,
  • replace the contents of this string with num ch characters, iterator start and end indicate the range.

For example, the following code shows the string "They say He carved it himself...find your soul-mate, Homer."

    string s = "They say he carved it himself...from a BIGGER spoon";
    string s2 = "find your soul-mate, Homer.";

    s.replace( 32, s2.length(), s2 );