• Practice5_2_string_find


    // Practice5_string.cpp : 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        /* 字符的遍历
        char* cArray = "2017/3/2 22:40";
        string str(cArray);
        //数组方式
        for(unsigned int i = 0; i < str.size(); i++)
        {
            cout << str[i] << " ";
        }
        cout << endl;
    
        //正向迭代器方式
        string::iterator it = str.begin();
        for(; it != str.end(); it++)
        {
            cout << *it << " ";
        }
        cout << endl;
    
        //反向迭代器方式
        string::reverse_iterator itReverse = str.rbegin();
        for(; itReverse != str.rend(); itReverse++)
        {
            cout << *itReverse << " ";
        }
        cout << endl;
        */
        string str("dog bird cat elephant chicken");
        
        //查找字符串
        cout << str.find("bird") << endl;
        cout << (int)str.find("pig") << endl;
    
        //查找字符
        cout << str.find('i', 7) << endl;
        
        //从末尾查找字符串
        cout << str.rfind("cat") << endl;
    
        //从末尾查找字符
        cout << str.rfind('c') << endl;
    
        //查找第一个属于某子串的字符
        cout << str.find_first_of("23d56") << endl;
    
        //查找第一个不属于某子串的字符
        cout << str.find_first_not_of("dog bird 2017") << endl;
    
        //查找最后一个属于某子串的字符
        cout << str.find_last_of("23d56") << endl;
        
        //查找最后一个不属于某子串的字符
        cout << str.find_last_not_of("tea") << endl;
        return 0;
    }

    运行结果:

    4
    -1
    24
    9
    25
    0
    9
    7
    28

  • 相关阅读:
    7月31号
    7月29号
    性能优化、微服务、并发编程、开源框架、分布式,面试你还缺什么
    Java 异常_2
    Java 异常浅学
    Java IO浅学
    Java File文件的读写
    Java BufferedReader 控制台输入
    Java 内部类
    Java 动静态绑定
  • 原文地址:https://www.cnblogs.com/liuzc/p/6494722.html
Copyright © 2020-2023  润新知