c++字符串string的操作

1.字符串的声明

#include <iostream>
#include <cstring>
#include <string>

using namespace std;

int main()
{
    //直接赋值
    string test;//定义了一个空字符串str
    test = "test_the_string";

    cout << "——————string字符串截取————————" << endl;

    //利用已有string初始化
    string s1(test); //复制字符串,s1=test,深拷贝
    cout << "s1 - " << s1 << endl;

    string s2(test, 6);//s2=test[6:]
    cout << "s2 - " << s2 << endl;

    string s3(test, 6, 3);//s3=test[6:(6+3)]
    cout << "s3 - " << s3 << endl;

    string s3_(test.begin()+6,test.begin()+9); //s3=test[6:(6+3)]
    cout << "s3_ - " << s3_ << endl;

    cout << "——————字符串数组截取————————" << endl;

    // C风格字符串转string初始化
    char testC[] = "test_the_string";  //定义了一个C字符串

    string s4(testC);  //s3=test
    cout << "s4 - " << s4 << endl;

    string s5(testC,6);  //s3=test[:7]
    cout << "s5 - " << s5 << endl;

    string s6(5,'A');  //生成一个字符串,包含5个'A'字符
    cout << "s6 - " << s6 << endl;

    cout << "——————字符串比较操作符————————" << endl;
    /* ==、>、<、>=、<=、和!=比较字符串(自左向右逐个按ASCII值大小相比较)
     * 可以用+或者+=操作符连接两个字符串,
     * 并可以用[]获取特定的字符 */

    bool temp = (s1 > "temper");//s>m
    cout << "s1 > s2 --- " << temp << endl;

    cout << "——————字符串比较操作符————————" << endl;
    /* string的一些特性
    int capacity()const;          //返回当前容量(即string中不必增加内存即可存放的元素个数)
    int max_size()const;          //返回string对象中可存放的最大字符串的长度
    int size()const;              //返回当前字符串的大小
    int length()const;            //返回当前字符串的长度
    bool empty()const;            //当前字符串是否为空
    void resize(int len,char c);  //把字符串当前大小置为len,多去少补,多出的字符c填充不足的部分*/

    cout << "s1是否为空:" << s1.empty() << endl;
    cout << "s1的长度为:" << s1.length() << endl;
    cout << "s1的当前容量(不新分配内存):" << s1.capacity() << endl;
    cout << "s1的最大容量(重新分配内存):" << s1.max_size() << endl;
    cout << "s1的长度:" << s1.size() << endl;
    s1.resize(20, '-');
    cout << "s1修改长度后,变成了:" << s1 << endl;

    cout << "——————字符串的查找————————" << endl;
    /* 查询结果类型为string::size_type,没有查询到,则返回string::npos;
     * 主要函数:find, rfind, find_first_of,find_last_of,s.find_first_not_of */

    cout << "->方法1,使用string::npos判断
";
    string::size_type loc;
    string s = "study hard and make progress everyday! every day!!";
    loc = s.find("make", 20);

    if(loc != string::npos){                  //搜不到为string::npos
        cout << "在" << loc << "找到" << endl;
    }
    else{
        cout << "没有找到元素" << endl;
    };

    cout << "->方法2,string::npos可强制转换为-1
";
    int local;
    local = static_cast<int>(s.find("make", 20));

    if(local != -1){                  //显式转换为int,搜不到为-1
        cout << "在- " << local << " -找到元素" << endl;
    }
    else{
        cout << "没有找到元素" << endl;
    };

    cout << "——————字符串的常用函数————————" << endl;

    cout << s1.erase(2,2) << endl << endl;      //【删除】第2个位置的后2个字符,返回删除后的结果
    cout << s1.insert(2, "st") << endl;         //在第2个位置【插入】字符串“st”
    cout << s1.append("尾部新添加的") << endl;   //【append】末尾添加
    cout << s1.replace(5, 3, "修改值") << endl; //索引5开始的3个字符【替换】成"修改值"
    cout << s1.substr(1, 4) << endl;            //【截取】字符串,s1[1:4]

    return 0;
}


——————string字符串截取————————
test_the_string
he_string
he_
he_
——————字符串数组截取————————
test_the_string
test_t
AAAAA

做得更好

原文地址:https://www.cnblogs.com/geoffreyone/p/9899735.html