心血来潮,写了个判断字符串是否包含的函数:
如果tszShortStr包含在tszLongStr中则返回从哪一位置开始包含,如果不包含返回-1
(仅返回第一次匹配的,假设用户的参数肯定正确)
请大家帮忙看看还能不能再优化一下:
int CInStrDlg::InStr(TCHAR *tszLongStr, TCHAR *tszShortStr)
{
int iLongLength = _tcslen(tszLongStr);
int iShortLength = _tcslen(tszShortStr);
int i = 0;
int j = 0;
int iEqual = 0; for(i = 0; i < iLongLength; i ++)
{
for(j = 0; j < iShortLength; j ++)
{
if(tszLongStr[i] == tszShortStr[j])
{
iEqual = 1;
}
else
{
iEqual = 0;
break;
}
}
if(iEqual == 1)
{
return i;
}
}
return -1;
}