CString search
-
I have 2 CStrings one has 2 dashs in it eg test1-test2-test3.txt the other one has 3 dashs in it. eg test1-test2-test3-test4.txt I need some code to find the one with the 3 dashs and kill the file. I'm in a while loop but and have been trying to get the find to work right I can find the first - but how do I continue on from there ? Thanks in advance for any help
-
I have 2 CStrings one has 2 dashs in it eg test1-test2-test3.txt the other one has 3 dashs in it. eg test1-test2-test3-test4.txt I need some code to find the one with the 3 dashs and kill the file. I'm in a while loop but and have been trying to get the find to work right I can find the first - but how do I continue on from there ? Thanks in advance for any help
Maybe just count the number of '-' in the string ...
CString testString = "test1-test2-test3--.txt";
int numberFound = 0;
for (int i=0; i<testString.GetLength(); i++)
if (testString.GetAt(i) == '-') numberFound++; -
Maybe just count the number of '-' in the string ...
CString testString = "test1-test2-test3--.txt";
int numberFound = 0;
for (int i=0; i<testString.GetLength(); i++)
if (testString.GetAt(i) == '-') numberFound++; -
I have 2 CStrings one has 2 dashs in it eg test1-test2-test3.txt the other one has 3 dashs in it. eg test1-test2-test3-test4.txt I need some code to find the one with the 3 dashs and kill the file. I'm in a while loop but and have been trying to get the find to work right I can find the first - but how do I continue on from there ? Thanks in advance for any help
int CountCharacters(CString sString, char cChar) { int nLen = sString.GetLength(); int nFound = 0; if (cChar == '\0' { nFound = nLen; } else { for (int nIndex = 0; nIndex < nLen; nIndex++) { nFound += ((sString[nIndex] == cChar) ? 1 : 0); } } return nFound; } //in the calling function, put this: if (CountCharacters(myString, '-') == 3) { // ... do something ... }
-
Phil first thanks I don't know if it was a typo but the i<testString would not compile I changed it to i&testString and then it compiled fine but it's just returning a UAB what is that ?
I am having problems formatting the "less than sign" (it thinks it is a start of a html tag). CString testString = "test-1test-2test3.txt"; int numberFound = 0; for (int i=0; i LESS_THAN_SIGN testString.GetLength(); i++) if (testString.GetAt(i) == '-')numberFound++; the GetLength() is a member function of the CString class
-
int CountCharacters(CString sString, char cChar) { int nLen = sString.GetLength(); int nFound = 0; if (cChar == '\0' { nFound = nLen; } else { for (int nIndex = 0; nIndex < nLen; nIndex++) { nFound += ((sString[nIndex] == cChar) ? 1 : 0); } } return nFound; } //in the calling function, put this: if (CountCharacters(myString, '-') == 3) { // ... do something ... }
-
I'm getting this error. any clues ? 'CountCharacters' : local function definitions are illegal
I think that you put the CountCharacters() function INSIDE of an existing function. Place it at the end of your CPP file (and remember to declare it at the top of the CPP file or in the .H file)
-
I'm getting this error. any clues ? 'CountCharacters' : local function definitions are illegal
-
I have 2 CStrings one has 2 dashs in it eg test1-test2-test3.txt the other one has 3 dashs in it. eg test1-test2-test3-test4.txt I need some code to find the one with the 3 dashs and kill the file. I'm in a while loop but and have been trying to get the find to work right I can find the first - but how do I continue on from there ? Thanks in advance for any help
Hi, Try this function to count the dashes:
int CountChar(CString str, TCHAR ch)
{
int found = 0;
int start = 0;
start = str.Find(ch, start);while (start => 0)
{
found++;
start = str.Find(ch, start);
}return found;
}I wrote this while replying, so I didn't try that. Should work... Paolo.