Android Question how to split a integer into its digits (integers)?

FJS

Active Member
Licensed User
Longtime User
Hello,

I am looking for a way to split a number (integer in this case) into its single digits

Example

number 12345

digit1=1
digit2=2
digit2=3
digit2=4
digit2=5

Thank you in advance for your help
 

Mahares

Expert
Licensed User
Longtime User
This is should do it foryou:
B4X:
Dim MyNumber As String="12345"
For i=0 To MyNumber.Length-1
    Log(MyNumber.SubString2(i,i+1))
Next
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
convert it to a string and then you can get each single digit

B4X:
Dim OrgValue As Int = 12345
Log("OrgValue = "&OrgValue) ' LOGs the int
Dim StrVal As String = OrgValue ' Define string and set the value to the int just defined
Log("StrVal = "&StrVal) ' LOGs the string
For i = 0 To StrVal.Length-1 ' For each character of the string
  Log("StrVal("&i&") = "&StrVal.SubString2(i,i+1)) ' log the character x from string...
Next
 
Upvote 0

mangojack

Well-Known Member
Licensed User
Longtime User
B4X:
Dim myNum As Int = 123456

   Dim myString As String = myNum
    For i  = 0 To myString.Length -1
       Log ( "Integer" & i & "=" & myString.SubString2 (i, i +1))
   Next

Edit ...... damn there's heavy traffic tonight .. lol:D but I'm the winner cause I went to 6
 
Upvote 0

FJS

Active Member
Licensed User
Longtime User
Thank to both of you (you are really fast, obviously)

however, the results are 5 strings, and I need 5 integers
In others words, I have an integer of 5 digits, and I would like to have 5 integers of only one digit.

How Can I do it??
 
Upvote 0

mangojack

Well-Known Member
Licensed User
Longtime User
there might be a better way ..
B4X:
Dim myNum As Int = 123456
 
Dim myString As String = myNum
Dim MyInt(myString.Length) As Int
 
For i  = 0 To myInt.Length -1
  MyInt(i) = myString.SubString2(i, i+1)
  Log(MyInt (i))
Next
 
Last edited:
Upvote 0

FJS

Active Member
Licensed User
Longtime User
Thank you for all your help,

Finally I whrote the next code:
B4X:
' here the a number is converted into its digits
'***********************************************
Dim myString As String = timevalue
Dim MyInt(myString.Length) As Int
digmax=3  ' the time has 3 digits: 020
    For i=0 To (digmax-1)
      Mtime(i)=0
    Next
    nn=0
    For i  =(digmax-MyInt.Length) To (digmax-1)
      Mtime(i) = myString.SubString2(nn, nn+1)
      nn=nn+1
    Next
'************************************************

Thanks to this, if I have a number of 3 digits like 023, I will have 0,2 and 3

Thanks for the clues!!
 
Upvote 0

mangojack

Well-Known Member
Licensed User
Longtime User
FJS ... I would like to understand your code better ... Can you tell me the value of 'timevalue' in the above example ? .
Does 'timevalue' always contain the same number of digits ?

Thanks
 
Upvote 0
Top