The string class provided by C + + contains a number of useful member functions that greatly facilitate the addition, deletion, change, query, and other operations of strings.
Insert String
The insert () function can insert another string in the position specified in the string string, one of its prototypes:
string Const string& str);
POS denotes the position to be inserted, which is the subscript; Str represents the string to be inserted, which can be a string variable or a C-style string.
Take a look at the following code:
#include <iostream>#include<string>using namespacestd;intMain () {stringS1, S2, S3; S1= S2 ="1234567890"; S3="AAA"; S1.insert (5, S3); cout<< S1 <<Endl; S2.insert (5,"BBB"); cout<< S2 <<Endl; return 0;}
The first parameter of the insert () function has the potential to cross out, and if it is out of bounds, a run-time exception is generated, and later I'll explain how to catch the exception.
Delete a string
The erase () function removes a substring from a string variable. One of its prototypes is:
string 0, size_t len = NPOs);
The POS represents the starting subscript for the substring to be deleted, and Len indicates the length of the substring to delete. If you do not specify Len, then delete all characters from Pos to the end of the string directly (len = Str.length-pos).
Take a look at the following code:
#include <iostream>#include<string>using namespacestd;intMain () {stringS1, S2, S3; S1= S2 = S3 ="1234567890"; S2.erase (5); S3.erase (5,3); cout<< S1 <<Endl; cout<< S2 <<Endl; cout<< S3 <<Endl; return 0;}
Some readers worry that the Len parameter may cause the substring to be dropped out of bounds if the POS parameter is not out of bounds. In fact, this does not happen, and the erase () function takes the smallest of the following two values as the length of the substring to be deleted:
- The value of Len;
- The string length minus the value of POS.
It's easier said than done. The deleted string can only be deleted to the end of the string.
Extract substring
The substr () function is used to extract substrings from string strings, which are prototyped as:
string 0 Const;
POS is the starting subscript for the substring to be extracted, and Len is the length of the substring to extract.
Take a look at the following code:
#include <iostream>#include<string>using namespacestd;intMain () {stringS1 ="First second third"; stringS2; S2= S1.substr (6,6); cout<< S1 <<Endl; cout<< S2 <<Endl; return 0;}
The system's handling of the substr () parameter is similar to the erase ():
- If the POS is out of bounds, an exception is thrown;
- If Len is out of bounds, all characters from Pos to the end of the string are extracted.
String Lookup
The string class provides several functions related to string lookups, as shown below.
Find () function
The Find () function is used to find where a substring appears in a string string, where two of its prototypes are:
size_t Find (conststring0const; size_t Find (constChar 0const;
The first parameter is the substring to be found, which can be a string variable or a C-style string. The second argument is the position (subscript) at which to start the lookup, or, if not specified, the No. 0 character.
Take a look at the following code:
#include <iostream>#include<string>using namespacestd;intMain () {stringS1 ="First second third"; stringS2 ="Second"; intindex = s1.find (s2,5); if(Index <s1.length ()) cout<<"Found at index:"<< Index <<Endl; Elsecout<<"Not found"<<Endl; return 0;}
The find () function ultimately returns the starting subscript for the first time a substring appears in a string. This example finally finds the S2 string at subscript 6. If the substring is not found, an infinity value of 4294967295 is returned.
RFind () function
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.
Take a look at the following example:
#include <iostream>#include<string>using namespacestd;intMain () {stringS1 ="First second third"; stringS2 ="Second"; intindex = s1.rfind (s2,6); if(Index <s1.length ()) cout<<"Found at index:"<< Index <<Endl; Elsecout<<"Not found"<<Endl; return 0;}
Find_first_of () function
The find_first_of () function is used to find the position of the first occurrence of a substring and a string in a string that have a character in common. Take a look at the following code:
#include <iostream>#include<string>using namespacestd;intMain () {stringS1 ="First second second third"; stringS2 ="Asecond"; intindex =s1.find_first_of (S2); if(Index <s1.length ()) cout<<"Found at index:"<< Index <<Endl; Elsecout<<"Not found"<<Endl; return 0;}
In this case, S1 and S2 together have the character is ' s ', the first occurrence of this character in S1 subscript is 3, so the search results returned 3.
C + + Learning to change the string strings