C++,STL 021(2024.09.29)

内容:从string字符串中获得想要的子串

(ps:明天白天要上课玩手机,白天的空闲时间要收行李,晚上又要去赶高铁,回到家应该会很晚了,有可能要被迫咕咕了啊)

#include <iostream>
#include <string>

using namespace std;

/*
1. string substr(int pos = 0, int n = npos) const;
(返回从pos位置开始的由n个字符组成的字符串,默认返回全部字符串)
2. 字符串首位置为0
*/

void test01()
{
    string str = "abcdefg";

    string str1 = str.substr(); // here
    cout << str1 << endl;

    string str2 = str.substr(1, 5); // here
    cout << str2 << endl;
}

// 操作案例:通过邮箱来获得用户名
void test02()
{
    string email = "xiaoming@qq.com";
    int pos = email.find('@');
    string name = email.substr(0, pos); // here
    cout << name << endl;
}

int main()
{
    test01();

    test02();

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值