A, C ++ strcpy string assignment function
strcpy(Cstring, value);
Cstring received value is the name of the variable, and the value is the name of a string constant, or another string variable C
#include <iostream> #include<string.h> using namespace std; int main() { const int SIZE = 12; char name1[SIZE], name2[SIZE]; strcpy(name1, "Sebastian"); cout << "name1:" << name1 << endl; strcpy(name2, name1); //将 name1 赋值给 name2 cout << "name2:" << name2 << endl; return 0; }
Two, C ++ Other common string handling functions:
- 1. append () - Add character at the end of the string
- 2. find () - Find a string in a string
- 4. insert () - Insert character
- 5. length () - returns a string length
- 6. replace () - replacement string
- 7. substr () - returns a substring
Example:
#include <the iostream> #include < string > the using namespace STD; int main () { // definition of a string class object string HTTP = " www.runoob.com " ; // print string length cout << http.length ( ) << endl; // stitching http.append ( " / C ++ " ); cout << endl << HTTP; // print results: www.runoob.com/C++ // delete int POS = http.find ( " / C ++ " ); // Find"C ++ "position in the string POS << << cout endl; http.replace (POS, 4 , "" ); // starting at position pos, 4 characters after replacement is empty, ie delete cout << HTTP << endl; // find child string runoob int First http.find_first_of = ( " . " ); // '.' character position from the beginning to find int Last http.find_last_of = ( " . " ); // '.' character from the beginning to find the position of the tail cout http.substr << (First + . 1 , Last-First- . 1 ) << endl; // extract "runoob" substring and print return 0 ; }
Three, stringstream common usage presentation
<Sstream> defines three classes: istringstream, ostringstream and the stringstream, it is used for the input stream, input and output operations and output
<sstream> primarily for data type conversion , since <sstream> using an array instead of the character string object (the snprintf mode), to avoid the risk of buffer overflow ; Also, since the type of the target object and passed parameters are automatically derived out so wrong character formatting problem does not exist. Simply put, compared with the data type conversion c library, <sstream> more secure, automatic and direct
(1) Data type conversion
#include < String > #include <sstream> #include <the iostream> #include <stdio.h> the using namespace STD; int main () { the stringstream sstream; String strResult; int nValue is = 1000 ; // will put an int the input stream sstream << nValue is; // extract a value of type int inserted before sstream from, the assigned string type sstream >> strResult; COUT << " [COUT] strResult iS: " << strResult << endl; printf( "[printf]strResult is: %s\n", strResult.c_str()); return 0; }
(2) stitching a plurality of strings
Stringstream stored in the plurality of strings, to achieve the purpose of splicing a plurality of strings (in fact, can be implemented using the string class), while the method described emptying of stringstream
#include <string> #include <sstream> #include <iostream> using namespace std; int main() { stringstream sstream; // 将多个字符串放入 sstream 中 sstream << "first" << " " << "string,"; sstream << " second string"; cout << "strResult is: " << sstream.str() << endl; Clear sstream// sstream.str(""); sstream << "third string"; cout << "After clear, strResult is: " << sstream.str() << endl; return 0; }
- May be used str () method to convert a string type stringstream type ;
- A plurality of strings can be placed in stringstream, Mosaic of the destination string;
- If you want to clear stringstream, you must use sstream.str ( ""); fashion;
- clear () method is suitable for multiple scene data type conversion, see the examples of the link
Details can poke link above
四、C++ isalpha、isalnum、islower、isupper
These functions are <the cctype> (i.e., C language <ctype.h> header file) inside
In general:
- the isalpha (letters, uppercase, lowercase)
- islower (lowercase)
- isupper (capital letters)
- isalnum (capital letters + numbers lowercase)
- ISBLANK (Space and \ t)
- isspace(space、\t、\r、\n)