关于string类中find函数的讲解

以下所讲的所有的string查找函数,都有唯一的返回类型,那就是size_type,即一个无符号整数(按打印出来的算)。若查找成功,返回按查找规则找到的第一个字符或子串的位置;若查找失败,返回npos,即-1(打印出来为4294967295)。

(1)string::find函数

#include<iostream>
#include<string>
using namespace std;
int main()
{
    //测试size_type find (charT c, size_type pos = 0) const noexcept;
    string st1("babbabab");
    cout << st1.find(‘a‘) << endl;//1   由原型知,若省略第2个参数,则默认从位置0(即第1个字符)起开始查找
    cout << st1.find(‘a‘, 0) << endl;//1
    cout << st1.find(‘a‘, 1) << endl;//1
    cout << st1.find(‘a‘, 2) << endl;//4   在st1中,从位置2(b,包括位置2)开始,查找字符a,返回首次匹配的位置,若匹配失败,返回npos
    cout << st1.rfind(‘a‘,7) << endl;//6   关于rfind,后面讲述
    cout << st1.find(‘c‘, 0) << endl;//4294967295
    cout << (st1.find(‘c‘, 0) == -1) << endl;//1
    cout << (st1.find(‘c‘, 0) == 4294967295) << endl;//1   两句均输出1,原因是计算机中-1和4294967295都表示为32个1(二进制)
    cout << st1.find(‘a‘, 100) << endl;//4294967295   当查找的起始位置超出字符串长度时,按查找失败处理,返回npos
    //测试size_type find (const basic_string& str, size_type pos = 0) const noexcept;
    string st2("aabcbcabcbabcc");
    string str1("abc");
    cout << st2.find(str1, 2) << endl;//6   从st2的位置2(b)开始匹配,返回第一次成功匹配时匹配的串(abc)的首字符在st2中的位置,失败返回npos
    //测试size_type find (const charT* s, size_type pos = 0) const;
    cout << st2.find("abc", 2) << endl; //6   同上,只不过参数不是string而是char*
    //测试size_type find (const charT* s, size_type pos, size_type n) const;
    cout << st2.find("abcdefg", 2, 3) << endl;//6   取abcdefg得前3个字符(abc)参与匹配,相当于st2.find("abc", 2)
    cout << st2.find("abcbc", 0, 5) << endl;//1   相当于st2.find("abcbc", 0)
    cout << st2.find("abcbc", 0, 6) << endl;//4294967295   第3个参数超出第1个参数的长度时,返回npos
    return 0;
}

(2)string::rfind()函数

rfind()与find()很相似,差别在于查找顺序不一样,rfind()是从指定位置起向前查找,直到串首。例如,上例中的st1.rfind(‘a‘,7)一句,就是从st1的位置7(st1的最后一个字符b)开始查找字符a,第一次找到的是倒数第2个字符a,所以返回6。

(3)string::find_first_of()函数

在源串中从位置pos起往后查找,只要在源串中遇到一个字符,该字符与目标串中任意一个字符相同,就停止查找,返回该字符在源串中的位置;若匹配失败,返回npos。

#include<iostream>
#include<string>
using namespace std;
int main()
{
    //测试size_type find_first_of (charT c, size_type pos = 0) const noexcept;
    string str("babccbabcc");
    cout << str.find(‘a‘, 0) << endl;//1
    cout << str.find_first_of(‘a‘, 0) << endl;//1   str.find_first_of(‘a‘, 0)与str.find(‘a‘, 0)
    //测试size_type find_first_of (const basic_string& str, size_type pos = 0) const noexcept;
    string str1("bcgjhikl");
    string str2("kghlj");
    cout << str1.find_first_of(str2, 0) << endl;//从str1的第0个字符b开始找,b不与str2中的任意字符匹配;再找c,c不与str2中的任意字符匹配;再找g,
                                                //g与str2中的g匹配,于是停止查找,返回g在str1中的位置2
    //测试size_type find_first_of (const charT* s, size_type pos, size_type n) const;
    cout << str1.find_first_of("kghlj", 0, 20);//2   尽管第3个参数超出了kghlj的长度,但仍能得到正确的结果,可以认为,str1是和"kghlj+乱码"做匹配
    return 0;
}

(4)string::find_last_of()函数

该函数与find_first_of()函数相似,只不过查找顺序是从指定位置向前

#include<iostream>
#include<string>
using namespace std;
int main()
{
    //测试size_type find_last_of (const charT* s, size_type pos = npos) const;
    //目标串中仅有字符c与源串中的两个c匹配,其余字符均不匹配
    string str("abcdecg");
    cout << str.find_last_of("hjlywkcipn", 6) << endl;//5   从str的位置6(g)开始想前找,g不匹配,再找c,c匹配,停止查找,返回c在str中的位置5
    cout << str.find_last_of("hjlywkcipn", 4) << endl;//2   从str的位置4(e)开始想前找,e不匹配,再找d,d不匹配,再找c,c匹配,停止查找,
                                                      //    返回c在str中的位置5
    cout << str.find_last_of("hjlywkcipn", 200) << endl;//5   当第2个参数超出源串的长度(这里str长度是7)时,不会出错,相当于从源串的最后一
                                                        //    个字符起开始查找
    return 0;
}

(5)string::find_first_not_of()函数

在源串中从位置pos开始往后查找,只要在源串遇到一个字符,该字符与目标串中的任意一个字符都不相同,就停止查找,返回该字符在源串中的位置;若遍历完整个源串,都找不到满足条件的字符,则返回npos。

#include<iostream>
#include<string>
using namespace std;
int main()
{
    //测试size_type find_first_not_of (const charT* s, size_type pos = 0) const;
    string str("abcdefg");
    cout << str.find_first_not_of("kiajbvehfgmlc", 0) << endl;//3   从源串str的位置0(a)开始查找,目标串中有a(匹配),再找b,b匹配,再找c,c匹配,
                                                              //    再找d,目标串中没有d(不匹配),停止查找,返回d在str中的位置3
    return 0;
}

(6)string::find_last_not_of()函数

find_last_not_of()与find_first_not_of()相似,只不过查找顺序是从指定位置向前

转自:https://www.cnblogs.com/zpcdbky/p/4471454.html

原文地址:https://www.cnblogs.com/mini-coconut/p/8977796.html

时间: 2024-08-07 07:14:55

关于string类中find函数的讲解的相关文章

c++中string类中的函数

C/C++ string库(string.h)提供了几个字符串查找函数,如下: memchr 在指定内存里定位给定字符 strchr 在指定字符串里定位给定字符 strcspn 返回在字符串str1里找到字符串str2里的任意一个字符之前已查找的字符数量 strrchr 在字符串里定位给定字符最后一次出现的位置 strpbrk 在字符串str1里定位字符串str2里任意一个首次出现的字符 strspn 返回字符串str1从开始字符到第一个不在str2中的字符个数 strstr 在字符串str1中

[C++]string类的查找函数

string类的查找函数: int find(char c, int pos = 0) const;//从pos开始查找字符c在当前字符串的位置 int find(const char *s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置 int find(const char *s, int pos, int n) const;//从pos开始查找字符串s中前n个字符在当前串中的位置 int find(const string &s, int pos =

【java&amp;c++】父子类中同名函数的覆盖问题

java和c++两门语言对于父子类中同名函数具有不同的处理方式. 先上两段代码: C++: class Basic { public: void test(string i){ cout << "basic str" <<endl; } void test(int i){ cout << "basic" <<endl; } }; class Senior : public Basic { public: void te

判断在类中某个函数(也可以是变量或类型)是否存在

一,判断在类中某个函数(也可以是变量或类型)是否存在 template<typename T> struct xxxx_detector { template<typename P,void (P::*)(void)> struct detector{}; template<typename P> static char func(detector<P,&P::init>*); template<typename P> static lo

String类中的常用方法_判断

字符串判断:1,字符串石佛包含某个字符串.boolean contains(str); 特殊之处:indexOf(String str),如果返回的值是-1,那么就表示不包含这个字符串 也可以实现判断. 2,字符串是否有内容.boolean isEmpty() 字符串为空时返回true 3,字符串是否以指定内容开头  boolean startsWith(String str) boolean startsWith(String str,int fromindex); 4,字符串是否以指定内容结

String 类中的常用方法_转换

1,将字符数组转换成字符串 使用构造函数,String(char[] charArry) String(char[] charArray,int offset,int count);将字符数组中的一部分转换成字符串.从offset取count个字符. static String copyValueOf(char[] charArry); static String copyValueOf(char[] charArry,int offset,int count); static String v

String类中的常用方法

String类 一.转换成String方法 1.public String(); 空参构造 初始化一个新创建的 String 对象,使其表示一个空字符序列 2.public String(byte[] bytes); 通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String 将97,97.99转成ascll码表对应的字符; 3.public String(byte[] bytes,int index,int length); 分配一个新的 String,使其表示字符数组参数

java中String类中的replace方法

package stringTest; public class StringDemo2 { public static void main(String[] args) { String s = "acvbb"; String s1 = new String("acvbb"); String s2 = s.replace("a", "X"); String s3 = s1.replace("a", &qu

String类中intern方法的原理分析

一,前言 ? 昨天简单整理了JVM内存分配和String类常用方法,遇到了String中的intern()方法.本来想一并总结起来,但是intern方法还涉及到JDK版本的问题,内容也相对较多,所以今天就弥补昨天缺失的知识点. 二,String.intern() ? 先来看下网上流行的关于intern()方法的示例代码: public static void main(String[] args) { String s = new String("1"); s.intern(); St