跳至主要內容

29.C++STL_string

约 110 字小于 1 分钟

#include "StringTest.h"

#include <iostream>
#include <ostream>
#include <string>

using namespace std;


StringTest::StringTest()
{
    string str = "StringTest";
    string s1(str);
    cout << s1 << endl;
    s1.assign("init");
    cout << s1 << endl;
    s1 += "_";
    s1.append("append");
    s1 += "_";
    cout << s1 << endl;

    int p = s1.find("n");
    cout << p << endl;
    p = s1.rfind("n");
    cout << p << endl;

    s1.replace(0, 1, "hello");
    cout << s1 << endl;


    int compare = s1.compare("hello");
    if (compare == 0)
    {
        cout << " equale " << endl;
    }
    else
    {
        cout << " not equle " << endl;
    }

    cout << s1.at(0) << endl;
    s1.at(0) = 'a';
    cout << s1.at(0) << endl;

    s1.insert(10, "sssssssss");
    cout << s1 << endl;

    s1.erase(0, 10);
    cout << s1 << endl;

    string s2 = s1.substr(0,10);
    cout << s2 << endl;

    
}

StringTest::~StringTest()
{
}