Tip: GetExtPart

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Tip: GetExtPart

Post by BackupUser »

Code updated for 5.20+ (same as GetExtensionPart())

Restored from previous forum. Originally posted by PB.

Until this command becomes native (hopefully!), you can use this procedure to
get the file extension from a given string. If no extension exists, the return
value is null.

Code: Select all

Procedure.s GetExtPart(file$)
  ;strip any trailing quotes.
  While Right(file$, 1) = Chr(34)
    file$ = Left(file$, Len(file$) - 1)
  Wend
  ;now get the extension.
  For r = Len(file$) To 1 Step -1
    If Mid(file$, r, 1) = "."
      ext$ = Mid(file$, r, 999)
      r = 1   ;to exit loop.
    EndIf
  Next
  ProcedureReturn ext$
EndProcedure

e$ = GetExtPart("c:\test.txt")   ;e$ will be ".txt"
Debug e$
PB - Registered PureBasic Coder

Edited by - PB on 02 May 2002 00:19:40