How to find number of commas(') in a given Cstring variable
Printable View
How to find number of commas(') in a given Cstring variable
Just use Find in a loop until the end of string
Consider:
giving:Code:#include <atlstr.h>
#include <stdio.h>
int count(const CString& cstr, char ch)
{
int cnt = 0;
for (int pos = -1; (pos = cstr.Find(ch, pos + 1)) >= 0; ++cnt);
return cnt;
}
int main()
{
const CString tst = ",qw,asd,zxc,";
printf("Number of ',' in '%s' is: %i\n", tst.GetString(), count(tst, ','));
}
Code:Number of ',' in ',qw,asd,zxc,' is: 4
thanks a lot..
Just in continuation with this.. how to trim a given cstring by removing all the double quotes " character in it.
example: "qw"as"dzxc" should become qwasdzxc
Use Remove(). See https://docs.microsoft.com/en-us/cpp...vs-2019#remove
Thanks for the response..