Pick a number from a value

This forum is currently in read-only mode.
From the Asset Store
Pick Up Items Sounds
$3 USD
50% off
Pick Up Items Sound effects for your game, Take them for a ride right now and you will worry no more.
  • 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?

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

    <img src="http://i163.photobucket.com/albums/t294/vinnyang/middleSubString.jpg">

    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

  • I understand perfectly! Thank you very much! Where do you find all these commands?

  • They're all expressions in the System object. There are further explanations at http://www.scirra.com/wiki/SystemExpressions

  • 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.

  • well, when string objects take in integers, they're automatically cast into string format are they not?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • It's not a big deal to put str() around a number. It's a string handling function after all.

  • 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.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)