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. Linux基础命令---调整程序优先级renice

    renice renice指令可以重新调整程序运行的优先级,可以通过进程id.用户id.组id来修改优先级.修改组的等级,影响组内所有用户的所有进程优先级:修改用户等级,影响该用户的所有进程优先级.除 ...

  2. 现代汽车加入Linux 基金会和 AGL协作平台

    1月4日,现代汽车宣布已加入 Linux 基金会和其旗下的非营利协作平台 Automotive Grade Linux(AGL),现代汽车公司副总裁兼信息娱乐技术中心负责人 Paul Choo 表示: ...

  3. spark读取hbase形成RDD,存入hive或者spark_sql分析

    object SaprkReadHbase { var total:Int = 0 def main(args: Array[String]) { val spark = SparkSession . ...

  4. django 动态生成PDF文件

    可以通过开源的Python PDF库ReportLab来实现PDF文件的动态生成. 一.安装ReportLab ReportLab库在PyPI上提供,可以使用pip来安装: $ pip install ...

  5. torchvision.datasets.ImageFolder数据加载

    ImageFolder 一个通用的数据加载器,数据集中的数据以以下方式组织 root/dog/xxx.png root/dog/xxy.png root/dog/xxz.png root/cat/12 ...

  6. mysqldump 使用小结

    语法: 备份某个数据库: mysqldump -uroot -p*** [options] –-databases DB_name > back_db_name.sql --databases: ...

  7. Docker学习笔记之docker volume 容器卷的那些事(二)

    预览目录 更改目录拥有者 Data Container 切换用户 参考文章 0x00 概述 如果你读了docker volume 容器卷的那些事(一),我想应该不会遇到下面这些问题的,毕竟是具有指导意 ...

  8. 10: VMware中扩展根分区

    1.1 添加一块硬盘 1.先给VMware添加一块60G硬盘 2.必须重启虚拟机才能识别到新加磁盘 fdisk -l        # 查看刚刚添加的硬盘 3.查看当前磁盘使用情况 df -hl   ...

  9. python简说(五)操作文件

    f = open('users.txt',encoding='utf-8') #读文件的时候,必须存在在才可以读 文件对象,或者文件句柄res = f.read()print(res)f.close( ...

  10. VC++ 获取exe或者dll版本信息

    #include <iostream> #include <atlstr.h> #pragma comment(lib,"version.lib") CSt ...