c++中截取想要的特定的字符子串

c++中想要对字符串string进行截取字符特定的字符字串,可以结合substr()和一系列的返回索引函数来做。

1、substr()函数很好理解,他的原型为:

//原型1
basic strin::substr(string,start,length);
//原型2(将string移到外面)
string &a,
a.substr(start,length);

函数功能时返回字符串a从start位置开始的长为length的子串,其中start参数必须提供,参数lmeiyouength为可选,如果没有提供length参数的话,将返回字符串a从start位置开始一直到字符串a的最后的子串。

2、读于索引查找函数,有多个:

//find()
size_t find (const string& str, size_t pos = 0) const noexcept;
//从源字符串起始位置pos(默认为0)处,查找有目标字符串str(string型字符串)的位置。

size_t find (const char* s, size_t pos = 0) const;
//从源字符串起始位置pos(默认为0)处,查找有目标字符串s(char型字符串)的位置。

size_t find (const char* s, size_t pos, size_type n) const;
//从源字符串起始位置pos处,查找有目标字符串s前n个字符(char型字符串)的位置。

size_t find (char c, size_t pos = 0) const noexcept;
//从源字符串起始位置pos(默认为0)处,查找有目标字符c的位置。


//find_first_of()
size_t find_first_of (const string& str, size_t pos = 0) const noexcept;
//从源字符串起始位置pos(默认为0)处,依此查找每个字符。如果某字符在目标字符串str(string型字符
//串)中,则返回首次匹配的该字符的位置。

size_t find_first_of (const char* s, size_t pos = 0) const;
//从源字符串起始位置pos(默认为0)处,依此查找每个字符。如果某字符在目标字符串s(char型字符串)
//中,则返回首次匹配的该字符的位置。

size_t find_first_of (const char* s, size_t pos, size_t n) const;
//从源字符串起始位置pos处,依此查找每个字符。如果某字符在目标字符串s前n个字符(char型字符串)
//中,则返回首次匹配的该字符的位置。

size_t find_first_of (char c, size_t pos = 0) const noexcept;
//从源字符串起始位置pos(默认为0)处,依此查找每个字符。如果某字符等于目标字符c,则返回首次匹
//配的该字符的位置。



//find_last_of()
size_t find_last_of (const string& str, size_t pos = npos) const noexcept;
//从源字符串起始位置pos(默认为npos)处,依此查找每个字符。如果某字符在目标字符串str(string型字
//符串)中,则返回首次匹配的该字符的位置。

size_t find_last_of (const char* s, size_t pos = npos) const;
//从源字符串起始位置pos(默认为npos)处,依此查找每个字符。如果某字符在目标字符串s(char型字符串)
//中,则返回首次匹配的该字符的位置。

size_t find_last_of (const char* s, size_t pos, size_t n) const;
//从源字符串起始位置pos处,依此查找每个字符。如果某字符在目标字符串s前n个字符(char型字符串)
//中,则返回首次匹配的该字符的位置。

size_t find_last_of (char c, size_t pos = npos) const noexcept;
//从源字符串起始位置pos(默认为npos)处,依此查找每个字符。如果某字符等于目标字符c,则返回首次匹
//配的该字符的位置。


//find_first_not_of()
size_t find_first_not_of (const string& str, size_t pos = 0) const noexcept;
//从源字符串起始位置pos(默认为0)处,依此查找每个字符。如果某字符不在目标字符串str(string型字符
//串)中,则返回首次不匹配的该字符的位置。

size_t find_first_not_of (const char* s, size_t pos = 0) const;
//从源字符串起始位置pos(默认为0)处,依此查找每个字符。如果某字符不在目标字符串s(char型字符串)
//中,则返回首次不匹配的该字符的位置。

size_t find_first_not_of (const char* s, size_t pos, size_t n) const;
//从源字符串起始位置pos处,依此查找每个字符。如果某字符不在目标字符串s前n个字符(char型字符串)
//中,则返回首次不匹配的该字符的位置。

size_t find_first_not_of (char c, size_t pos = 0) const noexcept;
//从源字符串起始位置pos(默认为0)处,依此查找每个字符。如果某字符不等于目标字符c,则返回首次
//不匹配的该字符的位置。


//find_last_not_of()
size_t find_last_not_of (const string& str, size_t pos = npos) const noexcept;
//从源字符串起始位置pos(默认为npos)处,依此查找每个字符。如果某字符不在目标字符串str(string型
//字符串)中,则返回首次不匹配的该字符的位置。

size_t find_last_not_of (const char* s, size_t pos = npos) const;
//从源字符串起始位置pos(默认为npos)处,依此查找每个字符。如果某字符不在目标字符串s(char型字符
//串)中,则返回首次不匹配的该字符的位置。

size_t find_first_not_of (const char* s, size_t pos, size_t n) const;
//从源字符串起始位置pos处,依此查找每个字符。如果某字符不在目标字符串s前n个字符(char型字符串)
//中,则返回首次不匹配的该字符的位置。

size_t find_last_not_of (const char* s, size_t pos, size_t n) const;
//从源字符串起始位置pos(默认为npos)处,依此查找每个字符。如果某字符不等于目标字符c,则返回首次
//不匹配的该字符的位置。


//rfind()
size_t rfind (const string& str, size_t pos = npos) const noexcept;
//从源字符串起始位置pos(默认为npos)处,查找有目标字符串str(string型字符串)的位置。

size_t rfind (const char* s, size_t pos = npos) const;
//从源字符串起始位置pos(默认为npos)处,查找有目标字符串s(char型字符串)的位置。

size_t rfind (const char* s, size_t pos, size_t n) const;
//从源字符串起始位置pos处,查找有目标字符串s前n个字符(char型字符串)的位置。

size_t rfind (char c, size_t pos = npos) const noexcept;
//从源字符串起始位置pos(默认为npos)处,查找有目标字符c的位置。

根据自己想要截取什么要的子串,取找合适的find函数,结合substr()函数就能得到自己想要的子串。

  • 3
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yisun03

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值