How to get a particular part of a String

前端 未结 3 1018
南笙
南笙 2020-12-18 06:56

I am writing a macro in Excel where I need to get a substring from a String. It\'s like this.

~/tester/test/hai/bye
~/stack/overflow/hai/bye
<
3条回答
  •  难免孤独
    2020-12-18 07:24

    You can do this using the InStr and Mid functions. Use the InStr function to find the occurrences of the / and then use Mid to get the part of the string that you are interested in.

    Try this:

    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
    

    This function will produce the desired results.

提交回复
热议问题