How to get character count
-
Hi all. How can i get a count of the characters present in a CString. i.e For ex: CString str = Sample("10,20,30,40,50"); I want to get the "," character count i.e 5 Any idea Thanks Raj
-
Hi all. How can i get a count of the characters present in a CString. i.e For ex: CString str = Sample("10,20,30,40,50"); I want to get the "," character count i.e 5 Any idea Thanks Raj
-
Hi all. How can i get a count of the characters present in a CString. i.e For ex: CString str = Sample("10,20,30,40,50"); I want to get the "," character count i.e 5 Any idea Thanks Raj
What's about this:
int GetCommaCount(const CString &str)
{
int count = 0;
for(int pos = 0; pos < str.GetLength(); pos++)
{
if (str[pos] == _T(','))
count++;
}
return count;
} -
Hi all. How can i get a count of the characters present in a CString. i.e For ex: CString str = Sample("10,20,30,40,50"); I want to get the "," character count i.e 5 Any idea Thanks Raj
-
Hi all. How can i get a count of the characters present in a CString. i.e For ex: CString str = Sample("10,20,30,40,50"); I want to get the "," character count i.e 5 Any idea Thanks Raj
std::count_if
is another method. Something likebool is_comma(char c)
{
return c == ',';
}std::string s = "1,2,3,4";
int count = std::count_if(s.begin(), s.end(), is_comma);
Edit for clarification: This algorithm can of course be applied to a CString as well using the GetBuffer() member.
modified on Thursday, August 26, 2010 5:31 AM
-
If you use the
CStringT
class you can do it via the SpanIncluding()[^] method.It's time for a new signature.
It's not so true because, as the MSDN documentation says, SpanIncluding returns an empty substring if the first character in the string is not in the specified set.
-
Hi all. How can i get a count of the characters present in a CString. i.e For ex: CString str = Sample("10,20,30,40,50"); I want to get the "," character count i.e 5 Any idea Thanks Raj
Assuming your string is
CString str = _T("Sample(10,20,30,40,50)");
Then
int start = -1;
int count = 0;
while ( (start = str.Find(_T(','), start + 1)) != -1)
count++;If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
std::count_if
is another method. Something likebool is_comma(char c)
{
return c == ',';
}std::string s = "1,2,3,4";
int count = std::count_if(s.begin(), s.end(), is_comma);
Edit for clarification: This algorithm can of course be applied to a CString as well using the GetBuffer() member.
modified on Thursday, August 26, 2010 5:31 AM
Of course you don't need
GetBuffer
:#include <algorithm>
using namespace std;
bool is_comma(TCHAR c)
{
return c==_T(',');
}LPCTSTR beg = (LPCTSTR) str;
LPCTSTR end = beg + str.GetLength();
int result = count_if(beg, end, is_comma);:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Of course you don't need
GetBuffer
:#include <algorithm>
using namespace std;
bool is_comma(TCHAR c)
{
return c==_T(',');
}LPCTSTR beg = (LPCTSTR) str;
LPCTSTR end = beg + str.GetLength();
int result = count_if(beg, end, is_comma);:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
It's not so true because, as the MSDN documentation says, SpanIncluding returns an empty substring if the first character in the string is not in the specified set.
-
Hi all. How can i get a count of the characters present in a CString. i.e For ex: CString str = Sample("10,20,30,40,50"); I want to get the "," character count i.e 5 Any idea Thanks Raj
raju_shiva wrote:
i.e For ex: CString str = Sample("10,20,30,40,50"); I want to get the "," character count i.e 5
Shouldn't that be 4? :confused:
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
-
Hi all. How can i get a count of the characters present in a CString. i.e For ex: CString str = Sample("10,20,30,40,50"); I want to get the "," character count i.e 5 Any idea Thanks Raj
You can use the CString::Tokenize[^] method by specifying "," as the token delimiter. You can get the count by incrementing a variable in the while loop in the example in the above link.
«_Superman_»
I love work. It gives me something to do between weekends.