string类

本文介绍了C++中字符数组、字符指针、字符串对象以及基本操作,如输出、复制、比较、交换、长度获取和字符串替换,并展示了如何使用`find`和`replace`方法进行操作。
摘要由CSDN通过智能技术生成

int main()
{
    //使用字符数组
    char str[]="123";
    cout << str << endl;
    //使用字符指针
    const char* s = "joker";
    cout << s << endl;
    //直接使用字面值
    cout<<"joker"<<endl;
    //使用字符串对象
    string s1("12z");
    string s2 = "123";
    string* s3 = new string("abcde");
    string s4 = s2;
    cout << s1 << "\t" << s2 << "\t" << *s3 << "\t" << endl;
    //复制
    s1 = s2;
    cout << s1 << endl;
    s4 += s2;
    cout << s4 << endl;
    //比较
    if (s1 == s4)
        cout << "yes"<< endl;
    s4.append("200");
    s4.push_back('!');
    cout << s4 << endl;
    //交换
    s1.swap(s4);
    cout << s1 << endl;
    cout << s4 << endl;
    //长度
    cout << s1.size() << endl;
    cout << s1.length()<< endl;
    //替换
    s1.replace(s1.find("32", 0), 6, "abcde");
    cout << s1 << endl;
    delete s3;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值