Android Question How to get each character from a string

Sreenadh OG

Member
Licensed User
Longtime User
Hai all,
I want to get each character from a string, my code is given below..
B4X:
Dim KeyPrev as string
KeyPrev="SomeText"
For i = 1 To KeyPrev.Length
       str = Mid(KeyPrev, i, 1)
Next
but this code not working properly, How can i get each character from a string??
 
Last edited:

RandomCoder

Well-Known Member
Licensed User
Longtime User
You will need to use CharAt(i)
Also bare in mind that the first charater starts at position 0 and so you will need to use...
B4X:
For i = 0 To KeyPrev.Length - 1
    str = KeyPrev.CharAt(i)
Next
 
Last edited:
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
Yet another way
B4X:
'I've not tested this, I've just typed it directly into this post, but it should work perfect.
     Dim StrTextArray(), StrText As String  
          StrText = "SomeText" 
          StrTextArray = Regex.Split("", StrText)
     Log(StrTextArray(0))     '0=S, 1=o, 2=m, 3=e etc. You can also loop through the array to reconstruct the original text if need be
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
After reading your response, I remembered about that 0 thing @sorex, my Regex splits start at 1 and not 0. Anyway I wrote that response on my mobile phone whilst in the park today, so I couldn't test it whilst getting a tan :)

Cheers for pointing that out, if I would have been in front of my laptop it would have clicked straight away. The Regex example was just to let screen know that there's more than one way to skin a cat...
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
Indeed Peter, it's all a habbit of using that method or the other. I was used to it from other languages to do it like that.
 
Upvote 0
Top