1. 截取子串

s.substr(pos, n)    截取s中从pos开始(包括0)的n个字符的子串,并返回

s.substr(pos)        截取s中从从pos开始(包括0)到末尾的所有字符的子串,并返回

2. 替换子串

s.replace(pos, n, s1)    用s1替换s中从pos开始(包括0)的n个字符的子串

3. 查找子串

s.find(s1)         查找s中第一次出现s1的位置,并返回(包括0)

s.rfind(s1)        查找s中最后次出现s1的位置,并返回(包括0)

s.find_first_of(s1)       查找在s1中任意一个字符在s中第一次出现的位置,并返回(包括0)

s.find_last_of(s1)       查找在s1中任意一个字符在s中最后一次出现的位置,并返回(包括0)

s.fin_first_not_of(s1)         查找s中第一个不属于s1中的字符的位置,并返回(包括0)

s.fin_last_not_of(s1)         查找s中最后一个不属于s1中的字符的位置,并返回(包括0)

一:find

函数原型:

size_t find ( const string& str, size_t pos = 0 ) const;
size_t find ( const char* s, size_t pos, size_t n ) const;
size_t find ( const char* s, size_t pos = 0 ) const;
size_t find ( char c, size_t pos = 0 ) const;

参数说明:pos查找起始位置    n待查找字符串的前n个字符

使用样例:

string  str1("the usage of find can you use it");

string  str2("the");

上面定义出了两个字符串;

str1.find(str2);                    // 从串str1中查找时str2,返回str2中首个字符在str1中的地址

str1.find(str2,5);                // 从str1的第5个字符开始查找str2

str1.find("usage");            //  如果usage在str1中查找到,返回u在str1中的位置

str1.find("o");                     // 查找字符o并返回地址

str1.find("of big",2,2);      //从str1中的第二个字符开始查找of big的前两个字符

二:find_first_of

函数原型:

size_t find_first_of ( const string& str, size_t pos = 0 ) const;
size_t find_first_of ( const char* s, size_t pos, size_t n ) const;
size_t find_first_of ( const char* s, size_t pos = 0 ) const;
size_t find_first_of ( char c, size_t pos = 0 ) const;

参数和find基本相同,不再赘述!

特别注意:

find_first_of 函数最容易出错的地方是和find函数搞混。它最大的区别就是如果在一个字符串str1中查找另一个字符串str2,如果str1中含有str2中的任何字符,则就会查找成功,而find则不同;

比如:

string str1("I am change");

string  str2("about");

int k=str1.find_first_of(str2);    //k返回的值是about这5个字符中任何一个首次在str1中出现的位置;
代码示例:
#include<iostream>
#include<string>
using namespace std; int main(){ string str1("hi,every one! I am heat_nan from dlut one");
string str2("heat_nan");
int k = str1.find(str2);
cout << "the position of 'heat_nan' is " << k << endl;
int k1 = str1.find("one");
cout << "the position of the first 'one' is " << k1 << endl;
int k2 = str1.find("one of", k1 + 1, 3);
cout << "the position of the second 'one' is " << k2 << endl;
int k3 = str1.find_first_of("aeiou");
while (k3 != string::npos){
str1[3] = '*';
k3 = str1.find_first_of("aeiou", k3 + 1);
}
cout << str1 << endl; system("pause");
return 0;
}

输出结果:

the position of 'heat_nan' is 19
the position of the first 'one' is 9
the position of the second 'one' is 38
hi,*very one! I am heat_nan from dlut one

参考

https://www.cnblogs.com/catgatp/p/6407788.html

https://blog.csdn.net/iot_change/article/details/8496977

string截取、替换、查找子串函数,find_first_of 用法的更多相关文章

  1. Oracle 截取、查找字符函数(持续更新)

    整理一些常用的截取.查找字符函数: 1.查找某一个字符串中某一个字符(串)出现的次数 SELECT LENGTH(REGEXP_REPLACE(REPLACE('anne<br>lily& ...

  2. C++基础-string截取、替换、查找子串函数

    1. 截取子串 s.substr(pos, n)    截取s中从pos开始(包括0)的n个字符的子串,并返回 s.substr(pos)        截取s中从从pos开始(包括0)到末尾的所有字 ...

  3. String字符串操作--切割,截取,替换,查找,比较,去空格.....

    字符串拼接 直接用+号:String a = "I"; String b = "love"; String c = "you";String ...

  4. python中string模块各属性以及函数的用法

    任何语言都离不开字符,那就会涉及对字符的操作,尤其是脚本语言更是频繁,不管是生产环境还是面试考验都要面对字符串的操作.     python的字符串操作通过2部分的方法函数基本上就可以解决所有的字符串 ...

  5. 牛人总结python中string模块各属性以及函数的用法,果断转了,好东西

    http://blog.chinaunix.net/uid-25992400-id-3283846.html http://blog.csdn.net/xiaoxiaoniaoer1/article/ ...

  6. [转]Python 字符串操作实现代码(截取/替换/查找/分割)

    原文地址:http://www.jb51.net/article/38102.htm ps:好久没更新python代码了,这次用到了字符串,转来看看 Python 截取字符串使用 变量[头下标:尾下标 ...

  7. Python 字符串操作(截取/替换/查找/分割)

    Python 截取字符串使用 变量[头下标:尾下标],就可以截取相应的字符串,其中下标是从0开始算起,可以是正数或负数,下标可以为空表示取到头或尾. # 例1:字符串截取 str = '1234567 ...

  8. python查找字符串 函数find() 用法

    sStr1 = 'abcdefg' sStr2 = 'cde' print sStr1.find(sStr2) 输出 2意思是在sStr1字符里的第2位置找到了包含cde字符的字段

  9. Python中字符串String的基本内置函数与过滤字符模块函数的基本用法

    Python中字符串String的基本内置函数与用法 首先我们要明白在python中当字符编码为:UTF-8时,中文在字符串中的占位为3个字节,其余字符为一个字节 下面就直接介绍几种python中字符 ...

随机推荐

  1. vue之component

    因为组件是可复用的 Vue 实例,所以它们与 new Vue 接收相同的选项,例如 data.computed.watch.methods 以及生命周期钩子等.仅有的例外是像 el 这样根实例特有的选 ...

  2. Django框架----ORM数据库操作注意事项

    1.多对多的正向查询 class Class(models.Model): name = models.CharField(max_length=32,verbose_name="班级名&q ...

  3. 微信h5支付“网站域名ICP备案主体与商户号主体不一致”的解决方法,H5微信支付 授权函下载

    如下图所示: 微信h5支付“网站域名ICP备案主体与商户号主体不一致”: 需提交H5微信支付 授权函 下载地址:https://download.csdn.net/download/a72400815 ...

  4. 远程图片转化为base64

    远程图片转化为base64 <?php /* * * 第一种方法 * 远程图片转化为base64,只支持http(推荐使用) * */ public static function imgUrl ...

  5. Linux使用退格键时出现^H解决方法

    以前在linux下执行脚本不注意输错内容需要删除时总是出现^H ,以前不知道真相的我没办法只有再重头运行一次脚本,后来发现其实时有解决办法的,所以记录一下. ^H不是H键的意思,是backspace. ...

  6. android 颜色值参考,(有颜色图

    ) 2011-10-13 19:55:30| 分类: android | 标签:android颜色值|字号大中小 订阅 Android 常用RGB值以及中英文名称   颜 色 RGB值 英文名 中文名 ...

  7. com.mchange.v2.c3p0.impl.NewPooledConnection@be1839d closed by a client的正确解答

    关于c3p0在debug模式下控制台抛出的如下异常: java.lang.Exception: DEBUG -- CLOSE BY CLIENT STACK TRACE at com.mchange. ...

  8. CIL锁,GIL与线程池的区别,进程池和线程池,同步与异步

    一.GIL锁 什么是GIL? 全局解释器锁,是加在解释器上的互斥锁 GC是python自带的内存管理机制,GC的工作原理:python中的内存管理使用的是应用计数,每个数会被加上一个整型的计数器,表示 ...

  9. Bootstrap3基础 pagination 分页按钮 简单示例

      内容 参数   OS   Windows 10 x64   browser   Firefox 65.0.2   framework     Bootstrap 3.3.7   editor    ...

  10. smbclient和mount -t cifs共享win的共享文件夹? autocad小记

    插入U盘没有反应? 首先,打开设备管理器, 发现usb大容量设备为黄色感叹号 其次, 将这个usb大容量设备先卸载, 然后点击"自动扫描硬件变化",就可以重新自动安装usb的驱动. ...