example: "select * from somewhere where x = 1
"
I want to find the whitespace-delimited "where
", but not the "where
" within "somewhere
". In the example "where" is delimited by spaces, but it could be carriage returns, tabs etc.
Note: I know regex would make it easy to do (the regex equivalent would be "\bwhere\b
"), but I don't want to add a regex library to my project just to do this.
If you wanted to use the pure MFC method of string manipulation, then this should work:
CString strSql = _T("select * from somewhere where x = 1");
int nTokenPos = 0;
CString strToken = strSql.Tokenize(_T(" \r\n\t"), nTokenPos);
while (!strToken.IsEmpty())
{
if (strToken.Trim().CompareNoCase(_T("where")) == 0)
return TRUE; // found
strToken = strSql.Tokenize(_T(" \r\n\t"), nTokenPos);
}
return FALSE; // not found
I don't know the first thing about MFC and CString
, but in plain C++ I'd stuff that string into an std::istringstream
and read from that into a string:
std::istringstream iss("select * from somewhere where x = 1");
std::string word;
do {
iss >> word;
if( !iss ) throw "blah blah!";
} while( word != "where" );
I suppose CString
does have stream operators overloaded with the appropriate semantics, but, as I said, I don't know it.
Pretty fast and doesn't need to construct heavy istringstream
objects.
CString str = L"select * from somewhere where x = 1";
CString to_find = L"where";
int i =0;
while ( i < str.GetLength() ) {
i = str.Find(to_find, i);
if ( i == -1 ) break;
if ( (i == 0 || !isalpha(str[i-1])) && (i == str.GetLength()-to_find.GetLength() || !isalpha(str[i+to_find.GetLength()])) ) break;
i+=to_find.GetLength();
}
if ( i >= 0 && i < str.GetLength() ) // found
else // not found
If you are using Visual Studio 2008 with Feature Pack you already have std::tr1::regex in C++.
Well, regex is the correct answer. If you don't want to include regex, you will have to try and implement a bit of regex yourself and search for " where ".
Your only other option I guess would be to search for "where", then check the character before and after the match to see if they are whitespace characters.