Wine Too
Wine Too

Reputation: 4655

Regex.Split Path

I have to split a path without file name to peaces. Since source of path may be from different OS I think the best will be to use Regex.

Examples of Path can be:

     Dim _path As String = "C:\First\Second\third"
     Dim _path As String = "C:\\First\Second\third/"
     Dim _path As String = "C:/First/Second/third\"
     Dim _path As String = "C:/First\Second\third"
     Dim _path As String = "C://First/Second/third"
     Dim _path As String = "usr/bin/first/second/third"
     Dim _path As String = "/usr/bin/first/second/third/"

... and other similar variations.

In short, path have to be splited by and in this order "//" OR "\\" OR "/" OR "\"

Wanted result of string array will be:

    Splitted(0) = "C:"
    Splitted(1) = "First"
    Splitted(2) = "Second"
    Splitted(3) = "Third"

    OR

    Splitted(0) = "usr"
    Splitted(1) = "bin"
    Splitted(2) = "First"
    Splitted(3) = "Second"
    Splitted(4) = "Third"

How to write those Regex.Split code in VB.NET?

Upvotes: 0

Views: 605

Answers (1)

Anton Krouglov
Anton Krouglov

Reputation: 3399

Best and fastest way is to use Split method instead of RegExp.

Dim Splitted As String() = _path.Split(New [Char]() {"\"c, "/"c},  StringSplitOptions.RemoveEmptyEntries)

Upvotes: 2

Related Questions