finding a string in a CString
-
can anyone please tell me what the simplest way is to see if a ceratin string is in another CString, for example: i have a CString with value: "coffee smells terrible" and i want to see if "err" is in that string(in "terrible"), how do i do that? thanx Kun
-
can anyone please tell me what the simplest way is to see if a ceratin string is in another CString, for example: i have a CString with value: "coffee smells terrible" and i want to see if "err" is in that string(in "terrible"), how do i do that? thanx Kun
You can use the Find function of CString. So you would have something like this:
int pos; CString buffer; buffer = "coffee smells terrible"; pos = buffer.Find("err"); if(pos != -1) { // found the string }
This should work for both MFC and WTL. Steve Maier, MCSD
-
You can use the Find function of CString. So you would have something like this:
int pos; CString buffer; buffer = "coffee smells terrible"; pos = buffer.Find("err"); if(pos != -1) { // found the string }
This should work for both MFC and WTL. Steve Maier, MCSD
-
can anyone please tell me what the simplest way is to see if a ceratin string is in another CString, for example: i have a CString with value: "coffee smells terrible" and i want to see if "err" is in that string(in "terrible"), how do i do that? thanx Kun
-
CString strCoffee = "coffee smells terrible"; int iSearch = strCoffee.find("err"); if (iSearch != -1) { //found it. } else { //couldn't find it. }
Ummm, you got the method name case wrong...
CString sBuffer(_T("The coffee smells terrible"));
if (sBuffer.Find("err") >= 0) // "Find" starts with a capital "F"
{
// string was found
}
else
{
// string not found
}Also, keep in mind that string searches in a CString are case-sensitive, so if that matters, you may want to use the CString::MakeLower() function befoire running your search.