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;
}


编译结果


原文地址:https://www.cnblogs.com/MCSFX/p/12975265.html