zoukankan      html  css  js  c++  java
  • 关于字符串find的一些函数

    1、find_first_of

    如果在一个字符串str1中查找另一个字符串str2,如果str1中含有str2中的任何字符,则就会查找成功,而find则不同;

    例如:word = "qwer" ;  yuan ="aueio" ;int index = word.find_first_of(yuan, x);  则index=2 x指的是从下表为x的开始查找

    2、find_first_not_of(const string &str,size_type index =0 ,size_type num)

    返回在字符串中首次出现的不匹配str中的任何一个字符的首字符索引, 从index开始搜索, 如果全部匹配则返回string::npos

    从index开始起搜索当前字符串, 查找其中与str前num个字符中的任意一个都不匹配的序列, 返回满足条件的第一个字符索引, 否则返回string::npos。

    3、sprintf

    //把整数123 打印成一个字符串保存在s 中。
    例如:sprintf(s, "%d", 123); //产生"123"

               char s[8];  sprintf(s, "%d", i);

    4、string::npos

    string:npos是个特殊值,说明查找没有匹配

    例如:查找字符串a是否包含子串b,strA.find(strB) != string:npos

    str.find("哦")==string::npos时则说明字符串str中不存在“哦”这个字符

    str.find("哦")!=string::npos则说明字符串str中存在“哦”这个字符

    5、rfind(const string& str, size_t pos = npos) const noexcept;

    从源字符串起始位置pos(默认为npos)处,查找有目标字符串str(string型字符串)的位置

    例如:size_t found2 = str1.rfind("two"); if (found2 != string::npos) { str1.replace(found2, strlen("two"), "TWO"); } cout<<str1<<endl;

    6、 find_last_of (const string& str, size_t pos = npos) const noexcept;

    从源字符串起始位置pos(默认为npos)处,依此查找每个字符。如果某字符在目标字符串str(string型字符串)中,则返回首次匹配的该字符的位置。

    例如:string str1("abcd efgh ijkl mnop qrst uvwx yz");  string str2("aeimqu");

    size_t found1 = str1.find_last_of(str2);

    while (found1 != string::npos) { str1[found1]='*'; found1 = str1.find_last_of(str2, found1-1); } cout<<str1<<endl;

    7、find_last_not_of (const string& str, size_t pos = npos) const noexcept;

    从源字符串起始位置pos(默认为npos)处,依此查找每个字符。如果某字符不在目标字符串str(string型字符串)中,则返回首次不匹配的该字符的位置。

    例如:string str1("abcd efgh ijkl mnop qrst uvwx yz");string str2("bcd fgh jkl nop rst vwx yz *");

    size_t found1 = str1.find_last_not_of(str2);

    while (found1 != string::npos) { str1[found1]='*'; found1 = str1.find_last_not_of(str2, found1-1); } cout<<str1<<endl;

  • 相关阅读:
    详细描述一下 Elasticsearch 索引文档的过程 ?
    elasticsearch 索引数据多了怎么办,如何调优,部署 ?
    elasticsearch 了解多少,说说你们公司 es 的集群架构,索 引数据大小,分片有多少,以及一些调优手段 ?
    Dubbo 和 Dubbox 之间的区别?
    Dubbo 支持服务降级吗?
    Pipeline 有什么好处,为什么要用 pipeline?
    为什么redis 需要把所有数据放到内存中?
    你对线程优先级的理解是什么?
    在 java 中 wait 和 sleep 方法的不同?
    一个线程运行时发生异常会怎样?
  • 原文地址:https://www.cnblogs.com/0211ji/p/13828633.html
Copyright © 2011-2022 走看看