I have an Edit Control (type: CString). How to count total number in this control? (Ex: 99->count:2; 000123456789 -> count:12)
Asked
Active
Viewed 870 times
0
-
Duplicate question, check out: http://stackoverflow.com/questions/6601592/counting-digits-using-while-loop – CppChris Apr 14 '14 at 15:36
-
@ChrisInked I don't see anything MFC-related in that question at all. – Lynn Crumbling Apr 14 '14 at 15:41
-
@ChrisInked: Log10 cannot solve my problem if zero(s) appear the first number (0000000003 -> count:10) not count:1 – Dark Light Apr 14 '14 at 15:57
-
zeros appear in your 000123456789 example, but you counted them. So it is not clear what you want. – ScottMcP-MVP Apr 14 '14 at 16:59
2 Answers
2
If you want to count the 0-9 digits in a CString
, you can simply use some code code like this:
int CountDigits(const CString& s)
{
int count = 0;
// For each character in the string
for (int i = 0; i < s.GetLength(); i++)
{
// If it's a digit (0,1,2,3,...9)
if (s[i] >= '0' && s[i] <= '9')
{
// Increment its count
count++;
}
}
return count;
}
Note that to check if a given TCHAR
in the CString
is a 0-9 digit, you may use _istdigit()
as well.

Mr.C64
- 41,637
- 14
- 86
- 162
0
Call into the GetWindowTextLength() member method to return the number of characters in the string.

Lynn Crumbling
- 12,985
- 8
- 57
- 95
-
sorry If you misunderstand, I mean count "digits" in CString, not count character – Dark Light Apr 14 '14 at 15:59
-
I'm confused. Your question was originally worded stating that you have an Edit control. You're going to need to get the text from this edit control to calculate a length. You specifically ask how to get data from the control. Now it seems that you're asking a completely different question, that really has absolutely nothing at all to do with an edit control, or retrieving data from it. It seems like your question is now related to CString's, and how to parse them for a numeric portion, in order to determine only the character count for that portion of the string. Is there sample data? – Lynn Crumbling Apr 14 '14 at 16:15
-
yes, just count digits in CString. Ex: abcdef5a1 ->count:2; 000aaa1 -> count:4; 0101aaa0101 -> count:8, that's all! – Dark Light Apr 14 '14 at 16:23
-
@DarkLight: I've provided an answer based in this comment of yours, but you may want to edit your question making it clearer. For example, since you have a CString, the information on the edit control is kind of useless. Moreover, giving an example like the one you gave in your comment above is helpful to clarify your goal. – Mr.C64 Apr 14 '14 at 16:49