Specific digit in integer or text

Get help using Construct 2

Post » Sat Jan 05, 2013 6:32 pm

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 ;)Tenshi2013-01-05 18:47:09
B
7
G
3
Posts: 46
Reputation: 1,581

Post » Sat Jan 05, 2013 7:03 pm

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
procrastinator2013-01-05 19:07:56
B
15
S
5
G
7
Posts: 877
Reputation: 5,650

Post » Sat Jan 05, 2013 7:26 pm

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.
Tenshi2013-01-05 19:33:36
B
7
G
3
Posts: 46
Reputation: 1,581

Post » Sat Jan 05, 2013 7:49 pm

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.
B
15
S
5
G
7
Posts: 877
Reputation: 5,650

Post » Sat Jan 05, 2013 7:57 pm

Yup, thanks.

Don't worry, it was clear with your first post and everything works fine ;)
B
7
G
3
Posts: 46
Reputation: 1,581

Post » Sat Jan 05, 2013 7:58 pm

ahh hahah seems I misread! Glad you "got it" ;p
B
15
S
5
G
7
Posts: 877
Reputation: 5,650

Post » Sat Jan 05, 2013 9:01 pm

To get the nth digit (0-based) of a number you just have to do:
[code](floor(x/(10^n))%10[/code]

For instance[code]The third digit of 123456
=(floor ( 123456/(10^2) ) %10)
= floor (123456/100) %10)
= floor (1234.56) % 10)
= 1234 % 10
= 4[/code]
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Sat Jan 05, 2013 9:07 pm

mid is easier to remember for us non-mathy people ;p but nice example!
B
15
S
5
G
7
Posts: 877
Reputation: 5,650


Return to How do I....?

Who is online

Users browsing this forum: christina, Yahoo [Bot] and 21 guests