Extract numbers from string
-
Hi I have strings like GROUP23 TREND12 GRAPH120 GRAPH01 From this i have to extract the numbers only like 23,12,120,01... I tried atoi(),but it returns 0. I tried Right(),
int iPanNo;
CString sNo;
CString sName = "GROUP23";
sNo = sName.Right(2);
iPanNo = atoi(sNo);The above code works fine and it returns '23'. But it fails for GRAPH120.For this it returns 20 not 120. How can i do that?
Anu
You need to parse your string into alpha and numeric portions and then apply the conversion on the numeric portion. Take a look at the methods of
CString()
to see if there is a builtin that will do it for you, or use something like isdigit()[^]. The rest should be easy.Just say 'NO' to evaluated arguments for diadic functions! Ash
-
Hi I have strings like GROUP23 TREND12 GRAPH120 GRAPH01 From this i have to extract the numbers only like 23,12,120,01... I tried atoi(),but it returns 0. I tried Right(),
int iPanNo;
CString sNo;
CString sName = "GROUP23";
sNo = sName.Right(2);
iPanNo = atoi(sNo);The above code works fine and it returns '23'. But it fails for GRAPH120.For this it returns 20 not 120. How can i do that?
Anu
Here is something you can try. You want only the numerals in the string and they appear after the alphabets. First you could use
CStringT::SpanExcluding
to extract only the alphabets in the string.CString sToken = sName.SpanExcluding(L"0123456789");
Next extract the characters after the length of the alphabets using
CStringT::Mid
.sNo = sName.Mid(sToken.GetLength());
«_Superman_»
I love work. It gives me something to do between weekends. -
Hi I have strings like GROUP23 TREND12 GRAPH120 GRAPH01 From this i have to extract the numbers only like 23,12,120,01... I tried atoi(),but it returns 0. I tried Right(),
int iPanNo;
CString sNo;
CString sName = "GROUP23";
sNo = sName.Right(2);
iPanNo = atoi(sNo);The above code works fine and it returns '23'. But it fails for GRAPH120.For this it returns 20 not 120. How can i do that?
Anu
I can see a couple of approaches here. The first way would be to pile your characters in a std::string, use find_first_of to locate where the digits and then use std::stringstream to convert from the digits to a number. That's a bit clunky though. You could go straight to using a std::stringstream, pile the characters in there and then peak at each character extracting non-digits. Once you hit a digit you could just extract the number. Cheers, Ash PS: If you're stuck using CString then use Superman's idea above
-
Hi I have strings like GROUP23 TREND12 GRAPH120 GRAPH01 From this i have to extract the numbers only like 23,12,120,01... I tried atoi(),but it returns 0. I tried Right(),
int iPanNo;
CString sNo;
CString sName = "GROUP23";
sNo = sName.Right(2);
iPanNo = atoi(sNo);The above code works fine and it returns '23'. But it fails for GRAPH120.For this it returns 20 not 120. How can i do that?
Anu
Anu_Bala wrote:
Hi I have strings like GROUP23 TREND12 GRAPH120 GRAPH01 From this i have to extract the numbers only like 23,12,120,01... I tried atoi(),but it returns 0. I tried Right(), int iPanNo; CString sNo; CString sName = "GROUP23"; sNo = sName.Right(2); iPanNo = atoi(sNo); The above code works fine and it returns '23'. But it fails for GRAPH120.For this it returns 20 not 120. How can i do that?
Didn't it occur to you that you would have to write some kind of crude loop to do that, at the least? Don't post such lazy assed efforts just to avoid scathing comments. These kinds of posts are far worse.
...byte till it megahertz... my donation to web rubbish
-
Hi I have strings like GROUP23 TREND12 GRAPH120 GRAPH01 From this i have to extract the numbers only like 23,12,120,01... I tried atoi(),but it returns 0. I tried Right(),
int iPanNo;
CString sNo;
CString sName = "GROUP23";
sNo = sName.Right(2);
iPanNo = atoi(sNo);The above code works fine and it returns '23'. But it fails for GRAPH120.For this it returns 20 not 120. How can i do that?
Anu
int begin = sName.FindOneOf("0123456789");
if (begin == -1)
{
// handle error
}
iPanNo = atoi((LPCSTR)s + begin);:)
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] -
You need to parse your string into alpha and numeric portions and then apply the conversion on the numeric portion. Take a look at the methods of
CString()
to see if there is a builtin that will do it for you, or use something like isdigit()[^]. The rest should be easy.Just say 'NO' to evaluated arguments for diadic functions! Ash
-
You need to parse your string into alpha and numeric portions and then apply the conversion on the numeric portion. Take a look at the methods of
CString()
to see if there is a builtin that will do it for you, or use something like isdigit()[^]. The rest should be easy.Just say 'NO' to evaluated arguments for diadic functions! Ash
-
Anu_Bala wrote:
Hi I have strings like GROUP23 TREND12 GRAPH120 GRAPH01 From this i have to extract the numbers only like 23,12,120,01... I tried atoi(),but it returns 0. I tried Right(), int iPanNo; CString sNo; CString sName = "GROUP23"; sNo = sName.Right(2); iPanNo = atoi(sNo); The above code works fine and it returns '23'. But it fails for GRAPH120.For this it returns 20 not 120. How can i do that?
Didn't it occur to you that you would have to write some kind of crude loop to do that, at the least? Don't post such lazy assed efforts just to avoid scathing comments. These kinds of posts are far worse.
...byte till it megahertz... my donation to web rubbish
-
compare each element of this string to the numeric range '0' to '9', if it's in the range, record it.
-
Hi I have strings like GROUP23 TREND12 GRAPH120 GRAPH01 From this i have to extract the numbers only like 23,12,120,01... I tried atoi(),but it returns 0. I tried Right(),
int iPanNo;
CString sNo;
CString sName = "GROUP23";
sNo = sName.Right(2);
iPanNo = atoi(sNo);The above code works fine and it returns '23'. But it fails for GRAPH120.For this it returns 20 not 120. How can i do that?
Anu
Read from right - to - left. Check if digit and set position for the left most digit. Deduct position from length and then you can get the length of your string.
-
Read from right - to - left. Check if digit and set position for the left most digit. Deduct position from length and then you can get the length of your string.