How do I specifically detect an x amount of decimals in a string

I'm trying to specifically detect an "x" which is "5", amount of digits in a string, and then convert and assign that value into another string.

And I'm not even sure if I should convert it into a string, so here's another question. Can "unsigned short" types be compared to integers? Or does it have to be strings.
So you've got a string, and somewhere in it are 5 consecutive digits, and you want to turn that into a string?

So you start with the string

hgjfdngjdfgjkdhg35275hjgfrhjg

and you want to get the string

35275 ?
If Moschops is right in his translation of your prompt...

Do a while loop that scrolls through the string until a char is between '0' and '9'. Mark it as your start variable. then continue scrolling through until a char is not between '0' and '9' and make that your end variable. Exit the loop.

Finally, run myNumberStr = bigString.substr(start, end - start);
The end - start portion is because the second argument is expecting a length of the sub-string, not a position value...

If you're clever about the function and the loop, you could set up the loop's begin value to be an input argument for the function which would allow for finding the next set of numbers in that same string the next time through.

I'm once again a little confused what your asking about in the second part of your prompt. Could you show an example of what you're trying to do?
The functions stoi() and to_string() found here might be what you're looking for-> http://www.cplusplus.com/reference/string/
Last edited on
Can "unsigned short" types be compared to integers?


Yes, unsigned short is just another type of integer, as is long and char.

So it sounds like you want to extract the string, convert to int(using the functions suggested by newbieg), then compare to another int.
Topic archived. No new replies allowed.