Android Question Split a number (1234...) into its characters (A=1 B=2 C=3 D=4 ... )

Kight Riot

Member
Licensed User
Longtime User
Hello all

I'm sure it is out there and I'm just not finding it, but I am trying to take a number and split it up. I will have up to a four digit number and do something with each of the characters. I know I get around it without this, but I am trying not to overly complicate the code.

Any help would be great thanks
Kight
 

ilan

Expert
Licensed User
Longtime User
as NJDude allready said u can use Substrings2

example:

B4X:
Dim result As Int
Dim number As String
number = "1234"

result = number.SubString2(0,1) 'result will be "1"
result = number.SubString2(0,2) 'result will be "12"
result = number.SubString2(2,3) 'result will be "3"
'...
 
Upvote 0

wl

Well-Known Member
Licensed User
Longtime User
It should be something like this (if you assume A = 0, B = 1, ...)

B4X:
    Dim lcv As Int
    Dim number As String = "1234567890"
    Dim ch As Int, sb As StringBuilder
    sb.Initialize
  
    For lcv = 0 To number.Length - 1
        ch = Asc(number.SubString2(lcv, lcv + 1))
        sb.Append (Chr (ch - 48 + 65))
    Next
  
    ToastMessageShow (sb.ToString, True)
 
Upvote 0

Kight Riot

Member
Licensed User
Longtime User
Thanks to all for your help! I still struggled getting it to work and wanted to post the full code to help someone else if the got stumped in the same spot. The problem was that the number was up to 4 digits, and would do odd things when it wasn't. Anyways, thanks again for the help and let me know if the code could have been done better. Here it is to explain a little better what I was going for.



B4X:
Dim number As String

    number = numDipValue

    'If the value of number = 2, then after NumberFormat2 it then is 0002
    number = (NumberFormat2(number, 4, 0, 0, False)) 'Forces the number to always be 4 digits

    Dim result1, result2, result3, result4 As Int

    'If the value of number = 0002
    result1 = number.SubString2(0,1)  'result1 = 0
    result2 = number.SubString2(1,2)  'result2 = 0
    result3 = number.SubString2(2,3)  'result3 = 0
    result4 = number.SubString2(3,4)  'result4 = 2

    If result1 = 0 Then
      ...
    Else
    If result1 = 1 Then
      ...


Thanks
Kight

P.S. This is the working code, very happy with its functionality.
 
Last edited:
Upvote 0

ilan

Expert
Licensed User
Longtime User
try this (not tested !!) you need to complete the rest by yourself ;)

B4X:
Private Sub Class_Globals
    dim resulstis as int 'this is the result that will call the sub
End Sub


sub button1_click 'button to check the result

dim res0 as int
dim pos as int
Dim number As String
    number = numDipValue
    'If the value of number = 2, then after NumberFormat2 it then is 0002
    number = (NumberFormat2(number, 4, 0, 0, False)) 'Forces the number to always be 4
    Dim result1, result2, result3, result4 As Int

    'If the value of number = 0002
    result1 = number.SubString2(0,1)  'result1 = 0
    result2 = number.SubString2(1,2)  'result2 = 0
    result3 = number.SubString2(2,3)  'result3 = 0
    result4 = number.SubString2(3,4)  'result4 = 2


for i = 1 to 4

  pos = i

  Select pos
      Case pos = 1
          res0 = result1
      Case pos = 2
          res0 = result2
      Case pos = 3
          res0 = result3
      Case pos = 4
          res0 = result4
  End Select

  for i2 = 0 to 9
    if res0 = i2 then
      resulstis = i2
      rescheck 'call sub rescheck and from there call another sub depens on result
    end if
  next


next

end sub


sub rescheck

  Select resulstis

      Case resulstis = 1
          doifresultis1
      Case resulstis = 2
          doifresultis2
      Case resulstis = 3
          doifresultis3
      Case resulstis = 4
          doifresultis4
      ....

  End Select

end sub


sub doifresultis1
... 'write what to do if result = 1
end sub


sub doifresultis2
.... 'write what to do if result = 2
end sub

....
 
Last edited:
Upvote 0

Kight Riot

Member
Licensed User
Longtime User
You can avoid multiple ifs by using 'select case'.
I added the last bit (If result1 = 0 ... ) with no expectation of help on it, but I gave it a shot and it cleaned up about 100 lines in that sub alone.

Everyone - Thanks for helping on the things that are asked and even more so on the things that are not.
 
Upvote 0
Top