(1) using:
The most common is:
# Include<Iostream>
Using NamespaceSTD;
More common:
# Include < String >
# Include < Iostream >
Using STD :: String ;
Using STD: CIN; // these three are in iostream
Using STD: Endl;
Using STD: cout;
Using STD: boolalpha; // explain this: the name of the string type of the output bool type is true or false.
Using STD: Getline ;//GetlineRead the entire line of text
// Getline: as longGetlineIn case of a line break, even if it is the first character of the input,GetlineWill also stop reading and return. // If the first character is a line breakStringThe parameter will be set to nullString.
(2)StringObject operations:
S. Empty ()IfSIf it is an empty string, returnTrueOtherwise, returnFalse.
S. Size ()ReturnSNumber of characters in
S [N]ReturnSCenter:NStarts from 0.
S1 + S2SetS1AndS2Concatenates a new string and returns the new string.
S1 = S2SetS1Replace contentS2Copy
V1 = v2ComparisonV1AndV2, ReturnsTrueOtherwise, returnFalse
The following is an example of using the above function:
# Include < String >
# Include < Iostream >
Using STD :: String ;
Using STD: CIN;
Using STD: Endl;
Using STD: cout;
Using STD: boolalpha;
Using STD: Getline;
Int Main ()
{
String Line;
String Strtmp;
// Read line at time until end-of-File
Cout < " Enter line: " ;
While (Getline (CIN, line ))
{
Cout < Line < Endl;
Cout < " Line Length: " < Line. Size () < Endl;
Cout < Boolalpha < Line. Empty ();
Cout < " The sixth character in the string: " < Line [ 5 ] < Endl; // The sixth character in the output string (starting from 0)
Cout < " Enter strtmp: " ;
CIN > Strtmp;
Cout < " Strtmp + line: " < Strtmp + Line < Endl;
Strtmp = Line;
Cout < Strtmp < Endl;
Cout < " Is strtmp equal to line? " < Boolalpha < (Strtmp = Line) < Endl;
Cout < " Enter line: " ;
}
Return 0 ;
}
(3)StringProcessing of characters in an object:
Isalnum (c)IfCIf it is a letter or number, it isTrue.
Isalpha (c)IfCIf it is a letter, it isTrue.
Iscntrl (c)IfCIs a control character, it isTrue
Isdigit (c)IfCIf it is a number, it isTrue.
Isgraph (c)IfCIt is not a space, but printable, it isTrue.
Islower (c)IfCIf it is a lowercase letter, it isTrue.
Isprint (c)IfCIs a printable character, it isTrue.
Ispunct (c)IfCIs a punctuation, thenTrue.
Isspace (c)IfCIs a blank character, it isTrue.
Isupper (c)IfCIs a capital letter, thenTrue.
Isxdigit (c)If yesCHexadecimal numberTrue.
Tolower (c)IfCReturns lowercase letters. Otherwise, the return value is directlyC.
Toupper (c)IfCIf it is a lower-case letter, it is returned in the upper-case letter format. Otherwise, it is returned directly.C.
Similarly, let's look at an example of using the above function:
# Include < String >
# Include < Iostream >
Using STD :: String ;
Using STD: CIN;
Using STD: Endl;
Using STD: cout;
Using STD: boolalpha;
Int Main ()
{
String Line;
Cout < " Enter line: " ;
While (CIN > Line)
{
Bool TMP;
TMP = Isalnum (line [ 0 ]); // All functions correspond to the above
Cout < Boolalpha < TMP < Endl;
TMP = Isalpha (line [ 1 ]);
Cout < Boolalpha < TMP < Endl;
TMP = Iscntrl (line [ 2 ]);
Cout < Boolalpha < TMP < Endl;
TMP = Isdigit (line [ 3 ]);
Cout < Boolalpha < TMP < Endl;
TMP = Islower (line [ 4 ]);
Cout < Boolalpha < TMP < Endl;
TMP = Isprint (line [ 5 ]);
Cout < Boolalpha < TMP < Endl;
TMP = Ispunct (line [ 6 ]);
Cout < Boolalpha < TMP < Endl;
TMP = Isspace (line [ 8 ]);
Cout < Boolalpha < TMP < Endl;
String Strtmp;
For ( Int I = 0 ; I < 10 ; I ++ )
{
Strtmp [I] = Toupper (line [I]);
Cout < Strtmp [I];
}
Cout < Endl;
Cout < " Enter line: " ;
}
Return 0 ; }
To be continued ...................