Android Question Occurrence of character in a string

Declan

Well-Known Member
Licensed User
Longtime User
I am attempting to obtain the "filename" from within a string.
In this example, the filename is:
B4X:
touchphotogrey.png
The complete string (MyGetFrom) received from a WebSocket is:
B4X:
https://api.appery.io/rest/1/db/files/58870186e4b07690b0043107/2003060e-5b47-440a-95ba-507b43de2c14.touchphotogrey.png
How do I "extract" the "touchphotogrey.png" from the "MyGetFrom" string?
I have tried:
B4X:
            Dim MyGetFrom As String = rxHW.Get(10)
            Dim ANS As List
            ANS = sf.split(MyGetFrom, ".")
            Log("My List: " & ANS)
            Dim ANSINT As Int
            ANSINT = ANS.Size
            Dim MyFileName As String = ANS(ANSINT -1)
But this not working
 

udg

Expert
Licensed User
Longtime User
In this specific case you may try a simple mystring.LastIndexOf(".") twice (using substrings)
 
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
Yes, I have seen that we can not split by "dot". Another solution could be to replace dots by "/" and then split by "/" and get the last two items

B4X:
Sub GetFileName (myURL as String) as String
 'Dim myURL As String = "https://api.appery.io/rest/1/db/files/58870186e4b07690b0043107/2003060e-5b47-440a-95ba-507b43de2c14.touchphotogrey.png"
 Dim SF As StringFunctions
 SF.Initialize
 Dim L0 As List
 L0.Initialize
 myURL=myURL.Replace(".","/")
 L0=SF.Split(myURL,"/")
 If L0.Size > 2 Then
 Return $"${L0.Get(L0.Size-2)}.${L0.Get(L0.Size-1)}"$
 End If
End Sub
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Summing up all above contributions:
B4X:
Dim MyGetFrom As String = "https://api.appery.io/rest/1/db/files/58870186e4b07690b0043107/2003060e-5b47-440a-95ba-507b43de2c14.touchphotogrey.png"   
    Dim values() As String = Regex.Split("\.", MyGetFrom)
    Dim MyFileName As String =values(values.Length -2) & "." & values(values.Length -1)
    Log(MyFileName)  'displays: touchphotogrey.png
 
Upvote 0
Top