B4J Question B4J - LastIndexOf

Declan

Well-Known Member
Licensed User
Longtime User
I am attempting to parse a string to extract the File name.
My Code:
B4X:
Sub btnFindEn_Action
    fc.setExtensionFilter("Image", Array As String("*.png", "*.png"))
    Dim f As String = fc.ShowOpen(MainForm)
   
      If f <> "" Then
        If f.EndsWith(".png") Then
            Dim MyPos As Int
            MyPos = f.LastIndexOf("/")
            Log(MyPos)
               Log(File.GetUri("Got: " & f, ""))
        End If
   End If
End Sub
For some reason, MyPos always returns "-1".
Log:
B4X:
Waiting for debugger to connect...
Program started.
-1
file:/C:/Software/B4J/ENCRYP~1/Objects/Got:%20C:/Test/test2.png
 

Declan

Well-Known Member
Licensed User
Longtime User
Put a breakpoint and check the value of f. Or log it with Log(f).

Tip: Use File.GetName to get the file name.
Thanks, now can get File name.

How do I get just the path to a selected Folder?
 
Last edited:
Upvote 0

billzhan

Active Member
Licensed User
Longtime User
B4X:
    Log(File.GetName(f))  'file name
    Log(File.GetFileParent(f))  'parent folder 
    GetCanonicalPath(f)   ' Windows short  file name to full name, \ to /

Sub GetCanonicalPath(Path As String) As String
   Dim fileO As JavaObject
   fileO.InitializeNewInstance("java.io.File", Array As Object(Path))
   Return fileO.RunMethod("getCanonicalPath", Null)
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…