• c++ string类find总结


    c++ string类的find:
    1.find

    string s = "My cat's breath smells like cat food.";
    int a=s.find("breath");
    cout<<a<<endl;


    因为find是从0数起,输出字符串第一个字符的索引(从0开始),所以a为9

    2.rfind

    string s = "My cat's breath smells like cat food.";
    int a=s.rfind("breath");
    cout<<a<<endl;


    因为rfind是从后部数起,还是输出字符串第一个字符的索引(从0开始)

    其实,find与rfind一样,都可以找完整的字符串和字符,下面来看看另外一些奇葩的函数

    3.find_first_of

    string s = "My cat's breath smells like cat food.";
    int a=s.find_first_of("beah");
    cout<<a<<endl;


    从0数起,但它只需找到一个与它串里一样的字符就输出来了
    答案是:4
    4.find_last_of

    string s = "My cat's breath smells like cat food.";
    int a=s.find_last_of("beah");
    cout<<a<<endl;


    从后部数起,而且和find_first_of一样,输出最后一个有一样的就好了
    答案是: 29

    5.find_first_not_of

    string s = "aaabaaaaaabaa.";
    int a=s.find_first_not_of('a');
    cout<<a<<endl;


    从0数起,但它只需找到一个与它串里不一样的字符就输出来了
    答案是:3

    6.find_last_not_of

    string s = "aaabaaaaaabaa";
    int a=s.find_last_not_of('a');
    cout<<a<<endl;


    从后部数起,而且和find_first_not_of一样,也是输出第一个(似乎是最后一个哈)有一样的就好了
    答案是: 10

    如果没找到的话会输出string::npos

  • 相关阅读:
    MarkDown学习
    做网站的或者博客的网站
    Calendar日历简单用法
    Cron表达式范例
    CSVFileUtil 读取写入CSV文件简单工具类
    直接读取ZIP包数据 线上、线下方法
    Spring @Async/@Transactional 失效的原因及解决方案
    多线程学习 读写锁
    多线程学习 公平锁和非公平锁
    多线程学习 ThreadLocal的使用。
  • 原文地址:https://www.cnblogs.com/hkaren/p/12356723.html
Copyright © 2020-2023  润新知