看流星社区

 找回密码
 注册账号
查看: 2345|回复: 0

查找CString里面出现某个字符的次数

[复制链接]

该用户从未签到

发表于 2014-6-16 17:37:07 | 显示全部楼层 |阅读模式
1、Replace函数替换查找

Replace函数返回值:返回被替换的字符数。如果这个字符串没有改变则返回零。

CString sTest="aabbccaadd";
int nCount=s.Replace("a","a");

nCount就是你的想要的值

CString::Replace
int Replace( TCHAR chOld, TCHAR chNew );
int Replace( LPCTSTR lpszOld, LPCTSTR lpszNew );

Return Value
The number of replaced instances of the character. Zero if the string isn't changed.


2、标准函数 count_if

#include <iostream>
#include <string>
#include <functional>
#include <algorithm>
using namespace std;

int main( void )
{
    const string a = " 12113";
    cout << count_if( a.begin(), a.end(), bind2nd(equal_to<char>(),'1') ) << endl;
    return 0;
}

CString也一样,但它没有标准的迭代器,因此需要写成
count_if( (LPCTSTR)a, (LPCTSTR)a+a.GetLength(), bind2nd(equal_to<TCHAR>(),_T('某字符')) )
点击按钮快速添加回复内容: 支持 高兴 激动 给力 加油 苦寻 生气 回帖 路过 感恩
您需要登录后才可以回帖 登录 | 注册账号

本版积分规则

小黑屋|手机版|Archiver|看流星社区 |网站地图

GMT+8, 2024-5-1 04:12

Powered by Kanliuxing X3.4

© 2010-2019 kanliuxing.com

快速回复 返回顶部 返回列表