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. centos下搭建Jenkins持续集成环境(安装jenkins)

    1.安装JDK yum install -y java 2.安装jenkins 添加Jenkins库到yum库,Jenkins将从这里下载安装. 1 wget -O /etc/yum.repos.d/ ...

  2. vue:vuex详解

    一.什么是Vuex? https://vuex.vuejs.org/zh-cn 官方说法:Vuex 是一个专为 Vue.js应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相 ...

  3. jquery操作节点

    var v= $("input[type='checkbox'][name='ids']:checked").closest('tr').find('td:eq(2)').map( ...

  4. Array和ArrayList不同

    Employee[] array = new Employee[10]; ArrayList<Employee> staff = new ArrayList<>(); 不同 A ...

  5. linux grep 正则表达式

    grep正则表达式元字符集: ^ 锚定行的开始 如:'^grep'匹配所有以grep开头的行. $ 锚定行的结束 如:'grep$'匹配所有以grep结尾的行. . 匹配一个非换行符的字符 如:'gr ...

  6. APIClound 弹出层 Frame

    JS api.openFrame({ name: 'showPic', url: './showPic.html', rect: { // x: api.pageParam.marginBottom, ...

  7. struts2 + spring + mybatis 框架整合详细介绍

    struts2 + spring + mybatis  框架整合详细介绍 参考地址: https://blog.csdn.net/qq_22028771/article/details/5149898 ...

  8. Caused by: java.lang.ClassNotFoundException: Illegal access: this web application instance has been stopped already. Could not load [org.jboss.netty.util.internal.ByteBufferUtil]. The following stack

    Caused by: java.lang.ClassNotFoundException: Illegal access: this web application instance has been ...

  9. JAVA的内存模型及结构

    所有的Java开发人员可能会遇到这样的困惑?我该为堆内存设置多大空间呢?OutOfMemoryError的异常到底涉及到运行时数据的哪块区域?该怎么解决呢? Java内存模型 Java内存模型在JVM ...

  10. linux上的文件服务

    主要的文件服务vsftp.Samba.NFS对比 服务器名称 用户客户端平台 使用范围 服务端口 VSFTP Windows/linux/unix/macOS等 发布网站,文件共享 Tcp/21 Sa ...