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

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/197952

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月12日 下午7:43
下一篇 2023年2月12日 下午7:43

相关推荐