1. Const char * Data ();
The data () function returns a pointer to its first character.
String str3 = "you are stupid! "; <Br/> char * Cc = (char *) str3.data (); <br/> * (CC + 1) = 'n '; <br/> cout <CC <Endl;
Because the data () function returns const char *, you cannot directly modify the value of the returned string through the pointer. to modify the value, you must convert the type to char *.
2. Const char * c_str ();
The c_str () function returns a pointer to a regular C string with the same content as this string.
It is similar to data () and returns a pointer to a constant string.
3. size_type copy (char * STR, size_type num, size_type index );
The copy () function copies its num characters to STR (starting from index ). The returned value is the number of copied characters.
It is a very useful function. Note that the STR parameter is generally the name of an array and pointer type parameters cannot be used because space needs to be pre-allocated.
Char dd [3]; <br/> str3.copy (DD, 3,0); <br/> cout <DD <Endl;
4. Int compare (const basic_string & Str );
Int compare (const char * Str );
Int compare (size_type index, size_type length, const basic_string & Str );
Int compare (size_type index, size_type length, const basic_string & STR, size_type index2,
Size_type leng22 );
Int compare (size_type index, size_type length, const char * STR, size_type leng22 );
The compare () function compares the string and STR in multiple ways and returns:
Return Value |
Situation |
Less Than Zero |
This <Str |
Zero |
This = Str |
Greater than zero |
This> Str |
Different functions:
- Compare yourself and STR,
- Compare your own substrings and str. The substrings start with the index, and the length is length.
- Compare your own substrings and STR substrings. index2 and leng2reference STR, index, and length reference your own substrings.
- Compare your own substrings and STR's substrings. The STR substrings start with index 0 and have the length of leng22. their own substrings start with index and have the length of length.
String str1 = "Hello world! ", Str2 =" Hello hell! "; <Br/> cout <(str1.compare (str2) = 0 )? "Equal": "Not equal") <Endl; <br/> cout <(str1.compare (, str2,) = 0 )? "Equal": "Not equal") <Endl; <br/> const char * str3 = "hellu sir! "; <Br/> cout <(str1.compare (, str3,) = 0 )? "Equal": "Not equal") <Endl;
5,
Find)
Syntax:
size_type find( const basic_string &str, size_type index ); size_type find( const char *str, size_type index ); size_type find( const char *str, size_type index, size_type length ); size_type find( char ch, size_type index ); |
Find () function:
- Returns the position where STR appears for the first time in the string (from index).If not found, returnString: NPOs,
- Returns the position where STR appears for the first time in the string (from index, Length: length ). If not found, returnString: NPOs,
- Returns the location where the character ch appears for the first time in the string (from index ). If not found, returnString: NPOs
String str1 ("Alpha Beta Gamma Delta"); <br/> unsigned int loc = str1.find ("Omega", 0); <br/> If (loc! = String: NPOs) <br/> cout <"Found Omega at" <loc <Endl; <br/> else <br/> cout <"didn't find Omega" <Endl; <br/>
6,
Substr
Syntax:
basic_string substr( size_type index, size_type num = npos ); |
Substr () returns a substring of this string, starting from index, with a length of num characters. If not specified, it will be the default valueString: NPOs. In this way, the substr () function will simply return the remaining strings starting with index.
String S ("what we have here is a failure to communicate"); </P> <p> string sub = S. substr (21); </P> <p> cout <"the original string is" <S <Endl; <br/> cout <"the substring is" <sub <Endl; <br/>
7,
Find_first_of
Syntax:
size_type find_first_of( const basic_string &str, size_type index = 0 ); size_type find_first_of( const char *str, size_type index = 0 ); size_type find_first_of( const char *str, size_type index, size_type num ); size_type find_first_of( char ch, size_type index = 0 ); |
Find_first_of () function:
- Find the first character in the string that matches a character in STR and return its position. The search starts from index and returns if it is not found.String: NPOs
- Find the first character in the string that matches a character in STR and return its position. You can search for a maximum of num characters starting from index. If not found, returnString: NPOs,
- Search for the first ch-matched character in the string and return its position. The search starts from index.
String str1 ("Alpha Beta Gamma Delta"); <br/> cout <str1.find _ first_of ("B", 7) <Endl; <br/> cout <string: NPOs <Endl;