Android Question Get Text

slo001

Member
hi
How can I get username and password values from the text below?

user:
Username: user4eir7yhm

    Password: uh6r0cdh
 

Sagenut

Expert
Licensed User
Longtime User
B4X:
Dim username As String = "Username: user4eir7yhm"
Dim password As String = "Password: uh6r0cdh"
Dim getUser() As String
getUser = Regex.Split(" ", username.Trim) 'Trim to eliminate blank spaces at the beginning and at the end of the string to parse
Dim getpw() As String
getpw = Regex.Split(" ", password.Trim) 'Trim to eliminate blank spaces at the beginning and at the end of the string to parse
Log(getUser(1))
Log(getpw(1))
It come obvious that all the Username and Password strings must have all the same format:
just ONE blank space after the ":" and no blank spaces inside the string.
 
Last edited:
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Another way to do it not necessarily better:
B4X:
Dim username As String = "Username: user4eir7yhm"
    Log(username.SubString(username.LastIndexOf(" ")))

B4X:
Dim password As String = "Password: uh6r0cdh"
    Log(password.SubString(password.LastIndexOf(" ")))

Edit: LastIndexOf(" ")) needs to be: LastIndexOf(" ")+1) to get rid of the space
 
Last edited:
Upvote 0

Filippo

Expert
Licensed User
Longtime User
B4X:
Dim username As String = "Username: user4eir7yhm"
Dim password As String = "Password: uh6r0cdh"
Dim getUser() As String
getUser = Regex.Split(" ", username.Trim) 'Trim to eliminate blank spaces at the beginning and at the end of the string to parse
Dim getpw() As String
getpw = Regex.Split(" ", password.Trim) 'Trim to eliminate blank spaces at the beginning and at the end of the string to parse
Log(getUser(1))
Log(getpw(1))
It come obvious that all the Username and Password strings must have all the same format:
just ONE blank space after the ":" and no blank spaces inside the string.

Your code, but only a bit optimized.
B4X:
    Dim username As String = "Username: user4eir7yhm"
    Dim password As String = "Password: uh6r0cdh"
    Dim getUser() As String
    getUser = Regex.Split(":", username)
    Dim getpw() As String
    getpw = Regex.Split(":", password)
    Log(getUser(1).Trim) 'eliminate all blank spaces
    Log(getpw(1).Trim) 'eliminate all blank spaces
 
Upvote 0
Top