Android Question How to get each character from a string

Discussion in 'Android Questions' started by Sreenadh OG, May 19, 2015.

  1. Sreenadh OG

    Sreenadh OG Member Licensed User

    Hai all,
    I want to get each character from a string, my code is given below..
    Code:
    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: May 19, 2015
  2. RandomCoder

    RandomCoder Well-Known Member Licensed 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...
    Code:
    For i = 0 To KeyPrev.Length - 1
        str = KeyPrev.CharAt(i)
    Next
     
    Last edited: May 19, 2015
    Devan, wonder and Sreenadh OG like this.
  3. Sreenadh OG

    Sreenadh OG Member Licensed User

    thank u.:)
     
    RandomCoder likes this.
  4. Sreenadh OG

    Sreenadh OG Member Licensed User

    sorry, I cant access ChrAt function, is there any library for string functions?
     
  5. klaus

    klaus Expert Licensed User

    It is part of the String !
    It's CharAt and not ChrAt.
    Chr = MyString.CharAt(i)
     
  6. Peter Simpson

    Peter Simpson Expert Licensed User

    Yet another way
    Code:
    '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
     
  7. RandomCoder

    RandomCoder Well-Known Member Licensed User

    Sorry, typed quickly on my lunch break at work. I've corrected my mistake now.
     
    Sreenadh OG likes this.
  8. sorex

    sorex Expert Licensed User

  9. sorex

    sorex Expert Licensed User

  10. Peter Simpson

    Peter Simpson Expert Licensed 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...
     
    RandomCoder likes this.
  11. sorex

    sorex Expert Licensed 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.
     
    Peter Simpson likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice