Pick a number from a value

For questions about using Classic.

Post » Thu Jan 17, 2008 4:02 am

I have a value, like 1558, and i want to get a specific number out of it. For example i want the third number. Is that possible?
B
8
S
2
G
5
Posts: 744
Reputation: 3,288

Post » Thu Jan 17, 2008 5:40 am

you need to first cast the number into a string format and then you need to extract its middle substring.



in this example i used 2 text objects, the first one is to show the original value in a string format (i stored the actual number in a variable inside the text object) and the second one is to show the extracted number but you can just as easily have done that with only one string object.

the middle substring method comes from the system object in this format:

Mid(String, Start, Count)

"Mid" is pretty obvious, it's the name of the method that's being called.

"String" is where you put the string, it can either be provided explicitly or it can come from an object as i have done in the picture but those aren't the only ways.

"Start" is the position you would want to substring to start extracting from beginning at the left.

"Count" is the number of characters you want to extract beginning from the start position.

the only thing you will have to be careful of is the ordering of the actions because certain actions aren't possible without other actions having taken place first.

i'd put up a picture of the actual outcome at runtime but it isn't much to look at, it's just a window with "1558" in one spot and "5" in another. XD

other than that, just play around with that method if you don't quite understand my essay of an explanation. lol
B
2
S
2
G
5
Posts: 293
Reputation: 2,236

Post » Thu Jan 17, 2008 6:43 am

I understand perfectly! Thank you very much! Where do you find all these commands?
B
8
S
2
G
5
Posts: 744
Reputation: 3,288

Post » Thu Jan 17, 2008 1:58 pm

They're all expressions in the System object. There are further explanations at http://www.scirra.com/wiki/SystemExpressions
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,600

Post » Thu Jan 17, 2008 3:20 pm

Could the string functions not take in integers as well to remove the casting step? Have it do it for you? As I understand in C++ this would be pretty easy to do.
Image Image
Scirra Founder
B
125
S
37
G
25
Posts: 3,945
Reputation: 44,922

Post » Thu Jan 17, 2008 3:31 pm

well, when string objects take in integers, they're automatically cast into string format are they not?
B
2
S
2
G
5
Posts: 293
Reputation: 2,236

Post » Thu Jan 17, 2008 3:57 pm

It's not a big deal to put str() around a number. It's a string handling function after all.
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,600

Post » Thu Jan 17, 2008 6:15 pm

ACTUALLY! if you check the image in my previous post, you'll notice that i actually didn't cast 1558 to a string, i actually displayed it as an integer and picked out the substring of the integer.

so i've just proved that it's possible to extract parts of an integer using the substring method but when it comes to using substring methods, it's better to cast whatever it is, that you want the substring of, to a string first though. this will prevent any unexpected surprises right from the start.
B
2
S
2
G
5
Posts: 293
Reputation: 2,236


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 0 guests