vc++ string Find problem
-
hi all I got the text from the popupmenu item using GetMenuString like so: TCHAR itemText[MAX_PATH] = {0}; GetMenuString(m_hList,i,itemText,MAX_PATH,MF_BYPOSITION); then i try to find if certain sequence is in the itemText like so: TCHAR strTOSearch[2]="("; if(_tcsstr(itemText,strToSearch)!=NULL) { .... } now if the strToSearch does not contain any white space, it works fine, but then strToSearch[4]="h h"; then it won't find the string even though itemText does contain "h h", what am I missing? thx koo9
-
hi all I got the text from the popupmenu item using GetMenuString like so: TCHAR itemText[MAX_PATH] = {0}; GetMenuString(m_hList,i,itemText,MAX_PATH,MF_BYPOSITION); then i try to find if certain sequence is in the itemText like so: TCHAR strTOSearch[2]="("; if(_tcsstr(itemText,strToSearch)!=NULL) { .... } now if the strToSearch does not contain any white space, it works fine, but then strToSearch[4]="h h"; then it won't find the string even though itemText does contain "h h", what am I missing? thx koo9
I did not used TCHAR yet. I did a simple test with strstr() and it worked.
char tst[50]; strcpy(tst,"Hello you!\nThis works!"); // put a string into tst if (strstr(tst,"lo y")) // strstr returns NULL if there is no ocurance m_output="OK"; UpdateData(FALSE);
The str-functions are pretty old and are not very object orientated but they don't let you down. I like them. ;) -
I did not used TCHAR yet. I did a simple test with strstr() and it worked.
char tst[50]; strcpy(tst,"Hello you!\nThis works!"); // put a string into tst if (strstr(tst,"lo y")) // strstr returns NULL if there is no ocurance m_output="OK"; UpdateData(FALSE);
The str-functions are pretty old and are not very object orientated but they don't let you down. I like them. ;)strstr and _tcsstr both works fine in ur case, I guess it's just my itemText from teh GetMenuString call, it's supposed to be LPTSTR, but I use TCHAR, then _tcsstr or strstr will cast it to char * in fact, const unsigned short * in the function, I guess that's why it does work. but not sure how get cast them correctly.
-
strstr and _tcsstr both works fine in ur case, I guess it's just my itemText from teh GetMenuString call, it's supposed to be LPTSTR, but I use TCHAR, then _tcsstr or strstr will cast it to char * in fact, const unsigned short * in the function, I guess that's why it does work. but not sure how get cast them correctly.
The best solution would be to use CString. It has many constructors. For LPCTSTR you can use CString::CString( LPCTSTR lpch, int nLength ) and for searching a substring or a character Value in another String use CString::CString::Find(...). Just check the CString class and you will see. If it still not work than look at the return value of GetMenuString. Maybe it redrives not the whole string (don't think so).
-
The best solution would be to use CString. It has many constructors. For LPCTSTR you can use CString::CString( LPCTSTR lpch, int nLength ) and for searching a substring or a character Value in another String use CString::CString::Find(...). Just check the CString class and you will see. If it still not work than look at the return value of GetMenuString. Maybe it redrives not the whole string (don't think so).