如何获取String的特定部分

Har*_*ish 4 excel vba excel-vba

我在Excel中编写一个宏,我需要从String中获取子字符串.就像这样.

~/tester/test/hai/bye
~/stack/overflow/hai/bye
Run Code Online (Sandbox Code Playgroud)

在上面的例子中,我需要从第一个中获取String测试器,从第二个中获取堆栈.我尝试过使用,InStr但没用.有人可以帮忙吗?

Run*_*tad 9

您可以使用InStr和Mid函数执行此操作.使用InStr函数查找/的出现次数,然后使用Mid获取您感兴趣的字符串部分.

试试这个:

Function ExtractFirstPartOfPath(path as String) as String

  Dim first, second as Integer

  first = InStr(path, "/")
  second = InStr(first + 1, path, "/")

  ExtractFirstPartOfPath = Mid(path, first + 1, second - first - 1)

End Function
Run Code Online (Sandbox Code Playgroud)

此功能将产生所需的结果.