在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

最佳答案

我认为使用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

关于vb6 - 获取vb6中字符串数组的最后一个元素?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1349950/

10-09 12:50