在CString中找到一个空格分隔的单词的最佳方式 [英] Best way to find a whitespace-delimited word in a CString
本文介绍了在CString中找到一个空格分隔的单词的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如: select * from where where x = 1
我想找到whitespace-分隔其中,而不是
其中 $ c>。在示例中,where由空格分隔,但它可以是回车,制表符等。
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.
注意:我知道regex会很容易做等价的将是 \bwhere \b
),但我不想添加一个regex库到我的项目,只是这样做。
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.
推荐答案
如果你想使用纯MFC方法的字符串操作,那么这应该工作:
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
这篇关于在CString中找到一个空格分隔的单词的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文