zoukankan      html  css  js  c++  java
  • c++中CString:: Find , ReverseFind, Left, Right

    
    

    CString 是在MFC中的头文件

    非MFC加上afx.h头文件

    直接上代码:

    // ConsoleApplication1.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include "iostream"
    #include "afx.h"
    
    using namespace std;
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    
        CString s1("abcab");
        ASSERT(s1.ReverseFind('b') == 4);
        ASSERT(s1.Find('b') == 1);
    
        
        CString s("abcaab");
        ASSERT(s.ReverseFind('b') == 5);   // 从右往左第一个b的索引是5
        ASSERT(s.Find('a') == 0);           // 从左往右第一个a的索引是0
        ASSERT(s.Find('b') == 1);          // 从左往右第一个b的索引是1
    
    
        CString str(_T("Shop,ap"));
        str = str.Left(4);
        cout << str << endl;    // str2 = Shop  从左字符串的个数
    
        CString str1(_T("Shop,ap"));
        str1 = str1.Right(1);
        cout << str1 << endl;   // str1 = p   从右字符串的个数
    
        CString str2(_T("Shop,ap"));   
        int idex = str2.Find('S');
        cout << idex << endl;   // idex = 0   按数组的索引从0开始
    
        CString str3(_T("Shop,ap"));  
        str3 = str3.Right(str3.GetLength() - 1 - str3.Find(','));
    
    
        cout << str3 << endl;
    
    
        getchar();
    
        return 0;
    }


    编译结果


  • 相关阅读:
    1242 斐波那契数列的第N项
    1256 乘法逆元
    1264 线段相交
    1265 四点共面
    Uva10881 Piotr's Ants
    hdu 5438 Ponds 长春网赛1002
    CodeForces 540D Bad Luck Island 概率dp
    hdu 1281 二分图残量增广
    hdu 2444判定二分图+最大匹配
    hdu 3416 Marriage Match IV
  • 原文地址:https://www.cnblogs.com/MCSFX/p/12975265.html
Copyright © 2011-2022 走看看