LastIndexOf Function

rtesluk

Member
Licensed User
Longtime User
Apr 14 2012
12:50 Hours

I though that this string function would only return -1 if the index was not found.

e.g.

B4X:
Sub SplitFF(S As String)
   Dim p As Int 
   Try
      p = S.LastIndexOf("/")
      'Msgbox(p,"")
      If p <> -1 AND p <> 64 Then
         CodeModule.sFolder = S.SubString2(0, p)
         CodeModule.sFileName = S.SubString(p + 1)
         CodeModule.sFileNameShortName = CodeModule.sFileName.SubString2(0, CodeModule.sFileName.IndexOf("."))
      End If
   Catch
      Msgbox(LastException.Message,"SplitFF Sub") 
   End Try
End Sub

if '/' is NOT found, S.LastIndexOf("/") returns 64 NOT -1.

What other error codes can this function return?

Ray Tesluk :sign0104:
 
Last edited:

klaus

Expert
Licensed User
Longtime User
I tried it and if the search pattern is not found it returns -1.
So if you get 64 that means that there is the search pattern at position 64.
You should either set a breakpoint or a Log in the code to check what happens.

Best regards.
 
Upvote 0

rtesluk

Member
Licensed User
Longtime User
Looking For A File With An Extension

April 15 2012
04:45 Hours

Thank you Klaus!!!

Code has been modified.

B4X:
Sub SplitFF(S As String)
   'Looking for a file with an EXTENSION
   'Albumthumb files have NO EXTENSIONS
   'e.g. /mnt/sdcard/Android/data/com.android.providers.media/albumthumbs/1332740980099
   '                                                                 *** 64 ***    
   '     01234567890123456789012345678901234567890123456789012345678901234
   Dim p, q As Int 
   Try
      p = S.LastIndexOf("/")
      If p <> -1 Then
         q = S.LastIndexOf(".")
         If q <> -1 Then
            If q > p Then
               CodeModule.sFolder = S.SubString2(0, p)
               CodeModule.sFileName = S.SubString(p + 1)
               'albumthumb file would CRASH here! except for condition: If q > p Then 
               CodeModule.sFileNameShortName = CodeModule.sFileName.SubString2(0, CodeModule.sFileName.IndexOf("."))
            End If
         End If
      End If
   Catch
      Msgbox(LastException.Message,"SplitFF Sub") 
   End Try
End Sub

Ray Tesluk :sign0104:
 
Upvote 0

atulindore

Member
Licensed User
Longtime User
Hi Erel
LastindexOf is not giving correct results for some strings ..
str = "/storage/sdcard1/DCIM/Year 2014/10842290_10205387227779801_457166052324295608_o.jpg"

pos = str.LastindexOf("/")

it is returning pos = 31

Pls guide ..
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
31 is the correct position
 
Upvote 0
Top