如何在visual basic 6中检索字符串数组的最后一个元素?
我正在处理具有多个点的文件名,这些点被拆分为一个数组,我只想操纵扩展名。以下代码有效,但有一个我想删除的硬编码元素。
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
答案 0 :(得分:28)
我认为使用Ubound应该可以解决问题:
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
答案 1 :(得分:4)
realExt = aPath(ubound(aPath))
答案 2 :(得分:0)
但是,如果它真的只是您之后的延伸,那么这就可以胜任:
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