面对现实,超越自己
逆水行舟,不进则退
posts - 269,comments - 32,trackbacks - 0
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('某字符')) )
posted on 2013-02-19 14:34 王海光 阅读(11710) 评论(2)  编辑 收藏 引用 所属分类: MFC

FeedBack:
# re: 查找CString里面出现某个字符的次数
2013-09-24 19:19 | JJ
第一种方法行不通,相同内容不计入替换次数,所以最终结果都是0.  回复  更多评论
  
# re: 查找CString里面出现某个字符的次数
2016-07-25 10:37 | 过客
@JJ
你注意看人家博主用的是哪个版本的重载函数,不是字符,是字符串。  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理