C + + basic string object

1, Basic use of string object

The string data type is provided in the C + + standard template library, which is specially used to process strings. String is a class. Variables of this type are called "string objects"

1. To use the string object in a program, you must include the header file string in the program, that is, at the beginning of the program, you need to add the following statement: ාinclude < string >

2. Declaring a string object is similar to declaring a normal variable in the following format: string variable name;

    string str1; //statement string object str1,Value is empty
    string city="Beijing"; //statement string object city,And use string constants for initialization
    string str2=city; //statement string object str2,And use string variables for initialization
    //You can also use character array pairs string Variable to initialize. for example:
    char name[ ]="C++program";
    string s1=name;
    //You can also declare a string Object array, that is, each element in the array is a string. For example:
    string citys[ ]={"Beijing","Shanghai","Tianjin","Chongqing"};
    cout<<citys[1]<<endl; //output Shanghai,Array subscript starts at 0
    cout<<sizeof(citys)/sizeof(string)<<endl; //Number of output array elements
    sizeof(string);//It's every string The size of the object, so sizeof(citys)/sizeof(string)Represents the number of array elements.

1. String connection: +

    string  s2="C++";
    string s3="C";
    cout<<"s2= "<<s2<<endl;//s2= C++
    cout<<"s3= "<<s3<<endl;//s3= C
    cout<<s3+s2<<endl; //CC++

2. Empty string judgment: empty()

    string str; //Uninitialized, empty string
        cout<<"str is NULL."<<",length="<<str.length()<<endl;//str is NULL.,length=0
    } else{
        cout<<"str is not NULL."<<endl;

3. Append string: append()

    string str; //Uninitialized, empty string
    cout<<"str is "<<str<<",size="<<str.size()<<endl;//str is ABCDEFG,size=7

4. Character lookup: find()

    string str; //Uninitialized, empty string
    cout<<"str is "<<str<<",size="<<str.size()<<endl;//str is ABCDEFG,size=7
    const char *p=str.c_str();
    //1,find Function return jk stay s Subscript location in
    cout<<"find:"<<str.find("D")<<endl; //Find successful, find:3
    cout<<"find:"<<str.find("F")<<endl; //Find successful, find:5
    //2,Returns the first occurrence and last occurrence of a substring in the parent string.
    cout<<"find_first_of:"<< str.find_first_of("A")<<endl;//find_first_of:0
    cout<<"find_last_of:"<< str.find_last_of("A")<<endl;//find_last_of:0
    //3,Find the position of a substring after a given location
    //From string str Subscript 4 start, find string D ,return D stay str Subscript in
    cout<<"find:"<<str.find("D",4)<<endl; //Find failed:find:4294967295
    //From string str Subscript 0 start, find string F ,return F stay str Subscript in
    cout<<"find:"<<str.find("F",0)<<endl; //Find successful, find:5

5. Character insertion: insert()

    string str; //Uninitialized, empty string
    //4,String insertion
    string str1=str.insert(4,"123");//Insert from subscript 4

Tags: C++

Posted on Sat, 23 May 2020 10:52:25 -0400 by somenoise