1、Replace函數(shù)替換查找
Replace函數(shù)返回值:返回被替換的字符數(shù)。如果這個字符串沒有改變則返回零。
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、標(biāo)準(zhǔn)函數(shù) 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也一樣,但它沒有標(biāo)準(zhǔn)的迭代器,因此需要寫成
count_if( (LPCTSTR)a, (LPCTSTR)a+a.GetLength(), bind2nd(equal_to<TCHAR>(),_T('某字符')) )