2009-08-28 9 views
12

Come si può recuperare l'ultimo elemento di una serie di stringhe in Visual Basic 6?Ottieni l'ultimo elemento dell'array di stringhe in vb6?

Ho a che fare con nomi di file con più punti che sono suddivisi in un array, e voglio manipolare solo l'estensione. Il seguente codice funziona, ma ha un elemento hardcoded che voglio rimuovere.

Private Sub Form_Load() 
    Dim aPath() As String 
    Dim FileName As String 
    Dim realExt As String 

    FileName = "A long dotty.file.name.txt" 
    aPath = Split(FileName, ".") 

    realExt = aPath(3) ' <-- how to not hardcode?' 

    MsgBox ("The real extension is: " & realExt) 
    Unload Me 
End Sub 
+1

Perché non trovare solo l'ultimo indice di "." personaggio nella stringa? –

risposta

25

Penso che utilizzando Ubound dovrebbe fare il trucco:

Private Sub Form_Load() 
    Dim aPath() As String 
    Dim FileName As String 
    Dim realExt As String 

    FileName = "A long dotty.file.name.txt" 
    aPath = Split(FileName, ".") 

    realExt = aPath(UBound(aPath)) 

    MsgBox ("The real extension is: " & realExt) 
    Unload Me 
End Sub 
3
realExt = aPath(ubound(aPath)) 
+0

- grazie mille! –

0

Tuttavia, se è davvero solo l'estensione che stai dopo, questo sarebbe fare il lavoro:

Private Sub Form_Load() 
    Dim sFileName As String 
    Dim lPos As Long 
    Dim sRealExt As String 

    sFileName = "A long dotty.file.name.txt" 
    lPos = InStrRev(sFileName, ".") 
    If lPos Then sRealExt = Mid$(sFileName, lPos + 1) 
End Sub