zoukankan      html  css  js  c++  java
  • string类型的查找

    sting s;

    记住典型的查找操作:

    s.find(args)                   

    s.find_fist_of(args)

    s.find_fist_not_of(args)

    还有3个逆序的查找操作,

    s.rfind(args)

    s.find_last_of(args)

    s.find_last_not_of(args)

    其中args可以为下面4个版本之一:

    c,pos

    s2,pos

    cp,pos

    cp,pos,n

    例子:

    /******************************************************************************
     * str.find(args)是完全匹配args中的字符串;
     * 注意:args是参数表(含要查找的字符或字符串参数),不是字符串!
     ********************************************************************************/
    
    #include <iostream>
    #include <string>
    
    using namespace std;
    int main()
    {
    	string str = "helloWorLD";
    	string::size_type pos1 = str.find("Wor");
    	cout << pos1 << endl;
    
    	string::size_type pos2 = str.find("woa");
    	if (pos2 == string::npos) {
    		cout << "Do not find" << endl;
    	} else {
    		cout << pos2 << endl;;
    	}
    	return 0;
    }
    

    输出:

    5

    Do not find

    /******************************************************************************
     * str.find_first_of(args)只要在str中找到args中的字符串参数中的任意字符就返回;
     * 注意:args是参数表(含要查找的字符或字符串参数),不是字符串!
     ********************************************************************************/
    
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    	string str = "he2l3l5oWo6rLD";
    	string::size_type pos = str.find_first_of("0123456789");
    	cout << pos << endl;
    	return 0;
    }
    

    输出:

    2

  • 相关阅读:
    python两个类之间变量和函数的调用
    ubuntu远程桌面设置
    ROS节点分布式运行方法
    pandaboard串口通信调试
    linux下查看cpu使用情况
    树莓派LED指示灯说明
    python多线程实践小结
    关系模型关系模型
    栈和队列的应用
    栈和队列
  • 原文地址:https://www.cnblogs.com/helloweworld/p/2824215.html
Copyright © 2011-2022 走看看