java stream findlast_C++string中find,find_first_of和find_last_of的用法

1.find

str.find(str1)

说明:从前向后在str中找到str1,并返回其索引值,否则返回-1

2.find_first_of

str.find_first_of(str1)

说明:从前向后在str中找到str1,并返回其索引值,否则返回-1

3.find_last_of

str.find_last_of(str1)

说明:从后向前在str中找到str1,并返回其从后向前的索引值,否则返回-1

#include

using namespace std;

int main(void)

{

string s = "一蓑烟雨任平生。";

int len = s.size();

int count = s.size() / string("一").size();

cout << "len = " << len << ", count = " << count << endl;

cout << "一: pos = " << s.find("一") << endl;

cout << "一: pos = " << s.find_first_of("一") << endl;

cout << "一: pos = " << s.find_last_of("一") << endl;

int pos = s.find("一", 1);

cout << "pos:" << pos << endl;

cout << "。: pos = "s.find("。") << endl;

cout << "。: pos = "s.find_last_of("。") << endl;

return 0;

}

30978d36fdf8cc486a4a51a2ea6f190d.png

结果:

len = 24, count = 8

一: pos = 0

一: pos = 0

一: pos = 22

pos:-1

0

2

总结:

在判断以中文字符结尾的字符串的时候最好不要用find_last_of这个函数,容易出错。如上例所示如果判断某句话是否以句号“。”结束,则需要判断返回值是否2,而不是判断返回值加上句号本身的长度等于该字符串的长度。

原文:https://www.cnblogs.com/zh20130424/p/11099932.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值