CString 字符串中搜索子串,但是大小写字母一视同仁,都查出来
CString 字符串中搜寻子串,但是大小写字母一视同仁,都查出来!
例如:CString a="hello world123";
搜索 HeLL 或者 Or 或者Llo 都要能搜出来,就是子串,但是大小写字母一视同仁都要搜到。。。
我用了Find函数,但是只能找到大小写都要一样的子串,用了CompareNoCase()可以忽视大小写,但是没办法组合到一起。。。
求各位大哥给出算法代码,帮我度过难关,谢谢大神,好人一生平安!
------解决思路----------------------
用个临时变量,将你的字串全部变成大写或小写,函数: MakeUpper 或 MakeLower 。
将你的要查的子串用函数根据上面条件,先变成大写或小写后再查找
例:
------解决思路----------------------
下面这个函数是以前编写的,项目中一直在使用。
/***
*function: special_stristr
*purpose:
* finds the first occurrence of string2 in string1 without regard to case.
*param in:
* char *string1 - string to search in
* char *string2 - string to search for
*param out: none
*return value:
* returns a pointer to the first occurrence of string2 in
* string1, or NULL if string2 does not occur in string1
*******************************************************************************/
char * __cdecl special_stristr(const char * str1, const char * str2)
{
CString strValue1, strValue2;
strValue1.Format("%s", str1);
strValue2.Format("%s", str2);
strValue1.MakeLower();
strValue2.MakeLower();
//strValue1.Find(strValue2);
return (strstr((char *)(LPCTSTR)strValue1, (char *)(LPCTSTR)strValue2));
}
例如:CString a="hello world123";
搜索 HeLL 或者 Or 或者Llo 都要能搜出来,就是子串,但是大小写字母一视同仁都要搜到。。。
我用了Find函数,但是只能找到大小写都要一样的子串,用了CompareNoCase()可以忽视大小写,但是没办法组合到一起。。。
求各位大哥给出算法代码,帮我度过难关,谢谢大神,好人一生平安!
------解决思路----------------------
用个临时变量,将你的字串全部变成大写或小写,函数: MakeUpper 或 MakeLower 。
将你的要查的子串用函数根据上面条件,先变成大写或小写后再查找
例:
a=a.MakeUpper();
b="Lio";
c=a.Find(b.MakeUpper());
------解决思路----------------------
下面这个函数是以前编写的,项目中一直在使用。
/***
*function: special_stristr
*purpose:
* finds the first occurrence of string2 in string1 without regard to case.
*param in:
* char *string1 - string to search in
* char *string2 - string to search for
*param out: none
*return value:
* returns a pointer to the first occurrence of string2 in
* string1, or NULL if string2 does not occur in string1
*******************************************************************************/
char * __cdecl special_stristr(const char * str1, const char * str2)
{
CString strValue1, strValue2;
strValue1.Format("%s", str1);
strValue2.Format("%s", str2);
strValue1.MakeLower();
strValue2.MakeLower();
//strValue1.Find(strValue2);
return (strstr((char *)(LPCTSTR)strValue1, (char *)(LPCTSTR)strValue2));
}