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环境 vi/vim ESC无法退出原因

    原因是输入模式是中文的,需要切换成英文半角符号输入命令!

  2. 使用SpringBoot的优势。

    Spring Boot 让开发变得更简单 Spring Boot 对开发效率的提升是全方位的,我们可以简单做一下对比: 在没有使用 Spring Boot 之前我们开发一个 web 项目需要做哪些工作 ...

  3. AtCoder Beginner Contest 082 B - Two Anagrams

    题目链接:https://abc082.contest.atcoder.jp/tasks/abc082_b Time limit : 2sec / Memory limit : 256MB Score ...

  4. 管理mycat命令详解

    mycat监听两个端口,分别为8066和9066:mycat服务默认的数据端口是8066,而9066端口则是mycat管理端口,用于管理mycat的整个集群状态.监听的端口可以在server.xml配 ...

  5. close yield

    close的方法主要是关闭子生成器,需要注意的有4点: 1.如果生成器close后,还继续next,会报错StopIteration   [图片]     2.如果我捕获了异常,将GeneratorE ...

  6. mysql用户的增删与密码丢失问题

    为root用户设置初始密码 mysqladmin -u root password 密码(单实例) mysqladmin -u root password 密码 -S /data/3306/mysql ...

  7. Spring Maven 包的依赖

    <properties> <spring.version>4.3.11.RELEASE</spring.version> </properties> & ...

  8. JS方法转字符串

    今天接手的代码比较特殊,需要动态拼接一个table,每一行<tr>都是通过转换为字符串,再拼接在一起放到tbody中的. 其中有的td标签中有a标签,需要给a标签添加点击事件,参数好多,动 ...

  9. JDK常用命令(一)jps、jstat

    曾几何时,我们学习java都不再研究jdk而直接使用IDEA.eclipse和Netbeans,仿佛我们就认为我们的程序是这些编辑器编译出来的,这时多么可笑.殊不知,编辑器就是方便我们编辑开发的,而真 ...

  10. Centos7.03搭建JDK、Tomcat、MySql环境

    本人linux服务器配置是:centos_7_03_64_20G_xdragon_20171025.vhd 公网IP:106.14.14.224 内存:2GB SecureCRT8.0:https:/ ...