Specific digit in integer or text

  • Hi,

    Someone knows how I can get the value of a specific digit in an integer or string ?

    (for exemple the first one, the last one, or even the 3rd one)

    Thanks ;)

  • For an integer..

    digit = mid( str( theInteger ), xpos, 1 ) )

    For a string...

    character = mid( theString, xpos, 1 )

    xpos = the position of the digit / character you want to retrieve. Remember 0 is the very first character, 1 the second and so on.

    Explained here -

    System Expressions

    About half way down the page under TEXT

  • I red this, but I'm not english and I didn't understood what was the "count" parameter (which is, in fact, totally obvious...).

    I thought "mid" was for counting the number of characters in a string from a point. ^^"

    Thanks for answering my stupid question. Now I presume I understand how Left and Right expressions works.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • mid returns the character in theString, at the position you specify (0 for first character etc), and count = how many characters you want returned, in this case 1.

    If you have a string "Hello world" and want the "w", then you'd do..

    theString = "Hello world"

    character = mid( theString, 6, 1 )

    If you wanted "Hell" returned (who would! ;) then..

    characters = mid( theString, 0, 4 )

    Hope that clears it up for you.

  • Yup, thanks.

    Don't worry, it was clear with your first post and everything works fine ;)

  • ahh hahah seems I misread! Glad you "got it" ;p

  • To get the nth digit (0-based) of a number you just have to do:

    (floor(x/(10^n))%10

    For instance

    The third digit of 123456
    =(floor ( 123456/(10^2) ) %10)
    = floor (123456/100) %10)
    = floor (1234.56) % 10)
    = 1234 % 10
    = 4
  • mid is easier to remember for us non-mathy people ;p but nice example!

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