Header file and namespace ::
# Include <string>
Using namespace STD;
Use the subscript type for traversal:
String: The size_type type describes the type of the string type lower mark. So the method for traversing the string type is:
For (string: size_type I = 0; I <obj. Size (); I ++)
// The I type cannot be determined because the length is unknown. Only the string: size_type type can be used.
{
Cout <OBJ [I] <Endl; // each member of the variable String object.
}
The const char * type pointer returned by obj. c_str () indicates that the maximum space cannot be assigned by other methods, that is, it cannot be
Left
. For protection purposes.
Important:
OBJ [I] indicates the I + 1 character in the OBJ string. Note:
OBJ [I] can be used as the left value.
. // Very important.
Note:
In some cases, the string class is equivalent to a character container. The string type also supports iterator operations.
.
Common Operation interfaces of the string class:
More operations of the string class
::
1. method for creating a String object:
String STR;
String STR ("Hello World ");
String STR = "Hello World ";
String STR (OBJ); string STR = OBJ;
2. Return the first address of the string in the string class: (return the first address of the string type, that is, the conversion from the C string, c_str
)
Const char * P;
P = Str. c_str ();
3. judge whether the string class object is null: (judge whether the string is null, empty
)
String STR;
Str. Empty (); // if it is null, true is returned. Otherwise, false is returned;
String Length and string storage capability
4. Obtain the number of characters in a string object: (default string length, size (), length, strlen (*. c_str ())
)
String: size_type size; // note that it must be of the size_type type. (The subscript type of string is size_type)
Str. Resize (number); // The default string stores 100 characters. But it is not actually 100, it does not mean that it can only store 100.
Size = Str. Size (); // obtain the default length of the STR string, which may not be so long.
Size = Str. Length (); // obtain the default length of the STR string, which may not be so long.
Example:
String STR;
Str. Resize (100 );
STR [0] = 'a ';
STR [1] = 'B ';
STR [2] = '/0 ';
Cout <Str. Size () <Endl; // The running result is 100.
Cout <Str. Length () <Endl; // The running result is 100.
Cout <strlen (Str. c_str () <Endl; // The running result is 2, and the true length of the string.
Add
5. Two Methods for string connection: (string connection, and append + and append
)
Method 1:
String str1 ("hello ");
String str2 ("world ");
Str1 +
= Str2; // It can also be str1 + = "world.
Method 2:
Str1.append
(Str2 );
6. Insert a string into the string: (insert a string, insert
)
String STR ("Hello World ");
Str. insert
(Pos, "billchen ");
Insert the string "billchen" at the position marked as pos In the STR string ".
Note:
Replacing a substring replace is equivalent to deleting a substring in erase, and then inserting a substring in insert.
Delete
7. delete a substring in a string: (delete the substring, erase
)
String STR ("Hello World ");
Str. Erase
(Pos, Len );
Delete a substring whose subscript starts from POs and is Len.
Note:
Clear the string using the clear () method.
Str. Clear ();
Change
8. replace a substring in a string: (replace the substring, replace
)
String str1 ("Hello World ");
Str1.replace
(Pos, Len, argS );
Delete a substring of the str1 string whose subscript starts from POs and its length is Len, and replace it with the string pointed to by args.
Query
9. Locate a substring in a string: (search for the specified substring in the string, find
)
String STR ("Hello World ");
Str. Find
("Wo"); // search for "wo" in Str"
The subscript of the first position.
Str. rfind
("Wo"); // search for the subscript of the last position of "wo" in Str.
String: size_type Ops = Str. Find ("wo ");
If (Ops = string: NPOs)
{
Cout <"not found" <Endl;
}
It is equivalent to the meaning of strchr and strrchr in C.
Str. find_first_of
(ARGs); // search for the first occurrence of any ARGs characters in Str.
Str. find_last_of
(ARGs); // search for the last occurrence of any ARGs character in Str.
Str. find_first_no_of
(ARGs); // in STR, find the first one that does not belong
ARGs characters.
Str. find_last_no_of
(ARGs); // in STR, find the last one that does not belong
ARGs characters.
Note:
The return values of the find method of the string class are size_type.
Type.
When not found, the returned value is string: NPOs;
You can compare the returned value with string: NOPs to determine whether to locate it.
Traversal
Method 1: subscript
11. Traverse every member of a string:
String STR ("Hello World ");
String: size_type I;
For (I = 0; I <= Str. Size (); I ++)
{
Cout <STR [I] <"";
}
Method 1: iterator
11. Traverse every member of a string:
String STR ("Hello World ");
String: iterator iter = Str. Begin ();
While (ITER! = Str. End ())
{
Cout <* ITER <"";
ITER ++;
}
Substring Truncation
12. truncate a substring from a string: (truncate a substring, substr
)
String STR ("Hello World ");
Str. substr (Pos, n); // capture a substring starting from POS with a length of N.
Str. substr (POS); // capture the substring starting from POs to the end of the string.
String relationship comparison
13. Judge the relationship between strings.
Equal =
Not Supported! =
Greater than, less than, and equal Relational operators maintain the original meaning.
The C ++ string class also provides the compare method to determine the relationship between strings.