Android Code Snippet [B4X] Hide full phone number

It does not always make sense to make the entire phone number visible. So we only show the prefix code and the last 4 characters. We replace the rest with *

Regex is unfortunately still a mytherium to me, hence this workaround:

B4X:
Private Sub HideFullPhoneNumber(PhoneNumber As String) As String
    Dim PasswordChars As String = ""
    For i = 0 To PhoneNumber.Length -1
        If i >= 3 And i <= PhoneNumber.Length -4 Then
            PasswordChars = PasswordChars & "*"
        End If
    Next
    Return PhoneNumber.SubString2(0,3) & PasswordChars & PhoneNumber.SubString2(PhoneNumber.Length -4,PhoneNumber.Length)
End Sub

B4X:
Log(HideFullPhoneNumber("+4918153451234"))
'+49********1234
 

Daestrum

Expert
Licensed User
Longtime User
Not sure if it works on Android, but Java 11+ allows for String repeats, would make your Sub just 1 line long
B4X:
 Return PhoneNumber.SubString2(0,3) & "*".As(JavaObject).RunMethod("repeat",array( PhoneNumber.Length - 7)) & PhoneNumber.SubString2(PhoneNumber.Length -4,PhoneNumber.Length)
 
Top