1. size_t find (const string& str, size_t pos = 0)

str.find(str1)

说明:从pos(默认是是0,即从头开始查找)开始查找,找到第一个和str1相匹配的子串,返回该子串的起始索引位置;如果没有找到则返回string::npos

参考:find函数:http://www.cplusplus.com/reference/string/string/find/

2. size_t find_first_of (const string& str, size_t pos = 0)

str.find_first_of(str1)

说明:从pos(默认是是0,即从头开始查找)开始查找,找到第一个和str1相匹配的子串,返回该子串的起始索引位置;如果没有找到则返回string::npos

参考:find_first_of函数:http://www.cplusplus.com/reference/string/string/find_first_of/

3.size_t find_last_of (const string& str, size_t pos = npos)

str.find_last_of(str1)

说明:从npos(默认是字符串最后一个,即从后向前查找)开始查找,找到第一个和str1相匹配的子串,返回该子串的最后一个字符的索引位置;如果没有找到则返回string::npos

参考: find_last_of函数:http://www.cplusplus.com/reference/string/string/find_last_of/

举例如下:

#include<iostream>
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 << "find:平: pos = " << s.find("平") << endl;
cout << "find_first_of:平: pos = " << s.find_first_of("平") << endl;
cout << "find_last_of:平: pos = " << s.find_last_of("平") << endl;
int pos = s.find("平", 9);
cout << "pos:" << pos << endl;
cout << "。: pos = " << s.find("。") << endl;
cout << "。: pos = " << s.find_last_of("。") << endl;
return 0;
}

结果:

len = 24, count = 8

find:平: pos = 15

find_first_of:平: pos = 15

find_last_of:平: pos = 17

pos:15

。: pos = 21

。: pos = 23

总结:

C++中一个中文字符(包括汉字和中文符号)在字符串中的长度是3。在一个字符

串中查找中文字符的时候最好不要用find_last_of,因为用find_last_of返回的

是这个中文字符串的最后一个pos,而不是第一个,极容易出错。如上述例子如果

判断一句话是否是以句号结尾,如果用find_last_of进行查找会得到23而一个句

号的长度是3,加起来会大于整个字符串的长度,主要原因还是容易忘记他是子

串的最后一个pos,如果用find或者find_first_of,出错概率较低。

C++string中find,find_first_of和find_last_of的用法的更多相关文章

  1. (转载)C++ string中find() ,rfind() 等函数 用法总结及示例

    string中 find()的应用  (rfind() 类似,只是从反向查找) 原型如下: (1)size_t find (const string& str, size_t pos = 0) ...

  2. C++string中用于查找的find系列函数浅析

    总述:      以下所讲的所有的string查找函数,都有唯一的返回类型,那就是size_type,即一个无符号整数(按打印出来的算).若查找成功,返回按查找规则找到的第一个字符或子串的位置:若查找 ...

  3. find_first_of()和 find_last_of() 【获取路径、文件名】

    find_first_of()和 find_last_of() [获取路径.文件名](2011-06-11 12:44:46)转载▼标签: 杂谈 分类: c  string 类提供字符串处理函数,利用 ...

  4. string中常用的函数

    string中常用的函数 发现在string在处理这符串是很好用,就找了一篇文章放在这里了.. 用 string来代替char * 数组,使用sort排序算法来排序,用unique 函数来去重1.De ...

  5. C++ string中的find()函数

    1.string中find()返回值是字母在母串中的位置(下标记录),如果没有找到,那么会返回一个特别的标记npos.(返回值可以看成是一个int型的数) #include<cstring> ...

  6. C++中 string 中的方法的使用详解

    string 字符串在所有的语言中都非常重要,c++也不例外,接下来我们将介绍string中的常用方法 1. size() 和 length() 函数 : 他们返回字符串的真实长度,且不会因为空格而截 ...

  7. Java的String中的subString()方法

    方法如下: public String substring(int beginIndex, int endIndex) 第一个int为开始的索引,对应String数字中的开始位置, 第二个是截止的索引 ...

  8. Here String 中不该进行分词

    我们知道,在 Shell 中,一个变量在被展开后,如果它没有被双引号包围起来,那么它展开后的值还会进行一次分词(word splitting,或者叫拆词,分词这个术语已经被搜索引擎相关技术占用了)操作 ...

  9. C++string中有关字符串内容修改和替换的函数浅析

    1.assign() 原型: //string (1) basic_string& assign (const basic_string& str); //substring (2) ...

随机推荐

  1. 禁用GPU版本TensorFlow,切换到CPU版本TensorFlow。

    #禁用gpu版本TensorFlow,因为CUDA号码从0开始,这里直接让CUDA使用-1的GPU,自然就无法使用gpu了. 代码前面加入: import osos.environ["CUD ...

  2. spring boot activiti 整合

    1.pom.xml <dependency> <groupId>org.activiti</groupId> <artifactId>activiti- ...

  3. Pytorch 多 GPU 并行处理机制

    Pytorch 的多 GPU 处理接口是 torch.nn.DataParallel(module, device_ids),其中 module 参数是所要执行的模型,而 device_ids 则是指 ...

  4. HTML让文字在图片上显示的几种方法

    第一种方式是image 作为背景图片,即:background:url("......."); 第二种方式是将img块与文字块(文字块采用span标签显示)放在同一个div 中,然 ...

  5. java接口的使用

    格式:public class SubImpl  extends Super  implements IA,IB 接口可以多继承,但是只能继承接口,不能继承类. 实现接口(支持多实现) [修饰符] c ...

  6. Vue的filter过滤器

    一和二,请参考https://www.cnblogs.com/zui-ai-java/p/11109213.html 三.index.html <!DOCTYPE html> <ht ...

  7. 关于 vue 生命周期 钩子函数 事件

    vue实例有一个完整的生命周期,也就是从开始创建.初始化数据.编译模板.挂载Dom.渲染->更新->渲染.卸载等一系列过程,我们称这是vue的生命周期. 通俗的将就是vue实例从创建到销毁 ...

  8. CodeForces 1204E"Natasha, Sasha and the Prefix Sums"(动态规划 or 组合数学--卡特兰数的应用)

    传送门 •参考资料 [1]:CF1204E Natasha, Sasha and the Prefix Sums(动态规划+组合数) •题意 由 n 个 1 和 m 个 -1 组成的 $C_{n+m} ...

  9. Web中的通配符

    /**的意思是所有文件夹及里面的子文件夹 /*是所有文件夹,不含子文件夹 /是web项目的根目录     http://www.coderanch.com/t/364782/Servlets/java ...

  10. 【58.75%】【BZOJ 1087】[SCOI2005]互不侵犯King

    Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 3040  Solved: 1786 [Submit][Status][Discuss] Descri ...