// strTest.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "strTest.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // The one and only application object CWinApp theApp; using namespace std; static int Occurs(CString cString, CString &cExpression) { int nPos = 0; int nOccured = 0; do { //Look for the search string in the expression nPos = cExpression.Find(cString,nPos); if (nPos < 0) { //This means that we did not find the item break; } else { //Increment the occured counter based on the current mode we are in nOccured++; nPos++; } } while (true); //Return the number of occurences return nOccured; } static int At(CString cSearchFor, CString &cSearchIn, int nOccurence) { //In this case we actually have to locate the occurence int i = 0; int nOccured = 0; int nPos = 0; //Loop through the string and get the position of the requiref occurence for (i=1;i<=nOccurence;i++) { nPos = cSearchIn.Find(cSearchFor,nPos); if (nPos < 0) { //This means that we did not find the item break; } else { //Increment the occured counter based on the current mode we are in nOccured++; //Check if this is the occurence we are looking for if (nOccured == nOccurence) { return nPos + 1; } else { nPos++; } } } //We never found our guy if we reached here return 0; } int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) { int nRetCode = 0; // initialize MFC and print and error on failure if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) { // TODO: change error code to suit your needs cerr << _T("Fatal Error: MFC initialization failed") << endl; nRetCode = 1; } else { CString sString, sTmp; // Replicate('a',100) sTmp = ","; for (int i = 0; i < 100; i++) { sTmp += 'a'; } for (i = 0; i < 20; i++) { sString += sTmp; } cout << "Lenght = " << sString.GetLength() << endl; int res; int t1 = GetTickCount(); for (int k = 1; k < 50000; k++) res = Occurs(",",sString); cout << "OCCURS " << GetTickCount()-t1 << endl; t1 = GetTickCount(); for (k = 1; k < 50000; k++) res = At(",",sString,20); cout << "AT " << GetTickCount()-t1 << endl; } return nRetCode; }>Normally one believes that the string functions of VFP are much fast;