Search code examples
stringvb.netgetfilepath

VB 2010 chop off part of string and get string up to next '\' in filepath


I cannot find a solution as how to chop off part of a string(filepath) and use what's left to get another string.

For example: if I have filepath: Q:\2456_blah_blah\file.txt and I want to put this entire filepath into an array and delete from left characters 0 through 8 (which is consistent for my application) so I'd have 'blah_blah\file.txt' left for my array.

(The blah_blah portion is going to be a dynamic length, but it will always be letters.)

So then I'd like to take the 'blah_blah\file.txt' and pull just from that beginning up to the next '\' symbol, getting "blah_blah" as a string. I'm not too savy with VB strings at this point, so any help would be much appreciated. This is just my thinking how it could be done, if there's a simpler solution, I'm all ears.

Thanks,


Solution

  • Two Substring calls should do it.

    Dim str As String = "Q:\2456_blah_blah\file.txt"
    str = str.Substring(8, str.Length - 8)
    Dim blah As String = str.Substring(0, str.IndexOf("\"))