(VB6)在某个点之前切片字符串

(VB6)在某个点之前切片字符串,vb6,substring,slice,Vb6,Substring,Slice,假设我有一个变量设置为图像的路径 Let img = "C:\Users\Example\Desktop\Test\Stuff\Icons\test.jpg" 我想使用Vb6在“\Icons”之前切片所有内容。因此,在对字符串进行切片后,它将仅为“\Icons\test.jpg” 我曾尝试在VB6中摆弄Mid$函数,但并没有取得多大成功。我知道,子字符串在vb6中不是可用的函数,但仅在vb.net中可用。在第一个\图标之后 path = "C:\Users\Example\

假设我有一个变量设置为图像的路径

Let img = "C:\Users\Example\Desktop\Test\Stuff\Icons\test.jpg"
我想使用Vb6在
“\Icons”
之前切片所有内容。因此,在对字符串进行切片后,它将仅为
“\Icons\test.jpg”

我曾尝试在VB6中摆弄
Mid$
函数,但并没有取得多大成功。我知道,子字符串在vb6中不是可用的函数,但仅在vb.net中可用。

在第一个\图标之后

path = "C:\Users\Example\Desktop\Test\Stuff\Icons\test.jpg"

?mid$(path, instr(1, path, "\icons\", vbTextCompare))

>  \Icons\test.jpg
或者在最后一个之后,应该有>1

path = "C:\Users\Example\Desktop\Test\Icons\Stuff\Icons\test.jpg"

?right$(path, len(path) - InStrRev(path, "\icons\", -1, vbTextCompare) + 1)

>  \Icons\test.jpg

通常使用
Split
函数很容易做到这一点。我编写了一个方法来演示它的使用,对于grins,它需要一个可选参数来指定要返回多少个目录。不传递数字将返回文件名,传递非常高的数字将返回完整路径(本地或UNC)。请注意,该方法中没有错误处理

Private Function GetFileAndBasePath(ByVal vPath As String, Optional ByVal baseFolderLevel = 0) As String
    Dim strPathParts() As String
    Dim strReturn As String
    Dim i As Integer

    strPathParts = Split(vPath, "\")
    Do While i <= baseFolderLevel And i <= UBound(strPathParts)
        If i > 0 Then
            strReturn = strPathParts(UBound(strPathParts) - i) & "\" & strReturn
        Else
            strReturn = strPathParts(UBound(strPathParts))
        End If
        i = i + 1
    Loop

    GetFileAndBasePath = strReturn

End Function
Private函数GetFileAndBasePath(ByVal vPath作为字符串,可选ByVal baseFolderLevel=0)作为字符串
将strPathParts()设置为字符串
变暗strReturn为字符串
作为整数的Dim i
strPathParts=Split(vPath,“\”)

使用
Instr
查找所需位置,然后使用
Mid
Left
Right
获取所需的子字符串portion@Plutonix谢谢我知道该怎么做了。我将Instr函数的值(返回字符串的起始位置)设置为一个变量,在Mid$函数中使用该变量来获取从我想要的字符串开始的所有文本。