-1

I am running into an issue when i split a string on "_Pub" and get the back half of the string it removes the first character and I don't understand why or how to fix it unless i add the character back in

 strFilePath = "/C:/Dev/Edge/_Publications/Ann Report/2013-2016/2016 Edge.pdf"

 Dim relPath = strFilepath.Split("_Publications")(1)
 lb.CommandArgument = relPath

returns Publications\Ann Report\2013-2016\2016 Edge.pdf

Sam Axe
  • 33,313
  • 9
  • 55
  • 89
Jacob Deskin
  • 103
  • 1
  • 14
  • 1
    Please show the exact input and output – Sami Kuhmonen Jan 12 '18 at 21:55
  • And if you do `(strFilepath.Split(“_Publications”))(1)`? – Sami Kuhmonen Jan 12 '18 at 22:00
  • Tried that and get the same output – Jacob Deskin Jan 12 '18 at 22:02
  • 1
    Possible duplicate of [VB.NET String.Split method?](https://stackoverflow.com/questions/7919253/vb-net-string-split-method) – Sami Kuhmonen Jan 12 '18 at 22:05
  • ...So add the character back in? You know exactly what it was... – cHao Jan 12 '18 at 22:07
  • I can yes i just didn't know if that would have been considered the "right" thing to do – Jacob Deskin Jan 12 '18 at 22:08
  • It's better than the alternative (basically using `IndexOf` and `Substring` to roll your own string splitter). – cHao Jan 12 '18 at 22:12
  • just trying to understand why it doesn't work as i expected it to – Jacob Deskin Jan 12 '18 at 22:12
  • 1
    Because most people, when they use a delimiter, don't want all their data polluted at the beginning or end with it. So when you split, you get the data on either side of the string/character (or between delimiters), but not the delimiters themselves.. – cHao Jan 12 '18 at 22:13
  • If you turn on Option Strict you would see what is going on. String.Split takes a Char(). You did not supply this explicitly so .Split took the first Char in your string as the splitter. It does not return the splitter as part of the resulting strings. – Mary Jan 13 '18 at 02:09
  • What you have as a delimiter is not a string array "string()" but a regular string. You need a string array to use a string as a delimiter. otherwise it takes the first char of your string. https://msdn.microsoft.com/en-us/library/tabh47cf(v=vs.110).aspx – Chillzy Jan 13 '18 at 02:53
  • Assuming that `strFilePath` is intended to represent a path, it should not start with a "/" and the path delimiter in Windows is "\", not "/". – Andrew Morton Jan 14 '18 at 12:14

3 Answers3

3

What you have as a delimiter is not a string array "string()" but a regular string. You need a string array to use a string as a delimiter. otherwise it takes the first char of your string.

https://msdn.microsoft.com/en-us/library/tabh47cf(v=vs.110).aspx

try this

Dim relPath = strFilepath.Split(new string() {"_Publications"}, StringSplitOptions.RemoveEmptyEntries)(1)
Chillzy
  • 468
  • 3
  • 9
0

It appears that you want to get the part of the path starting at some directory. Splitting the path might not be such a good idea: imagine if there was a file "My_Publications_2017.pdf" in a directory "C:\Dev\Edge\_Publications". The split as you intended in the question would give the array of strings {"C:\Dev\Edge\", "\My", "_2017.pdf"}. As has been pointed out elsewhere, the String.Split you used doesn't do that anyway.

A more robust way would be to find where the starting directory's name is in the full path and get the substring of the path starting with it, e.g.:

Function GetRelativePath(fullPath As String, startingDirectory As String) As String
    ' Fix some errors in how the fullPath might be supplied:
    Dim tidiedPath = Path.GetFullPath(fullPath.TrimStart("/".ToCharArray()))

    Dim sep = Path.DirectorySeparatorChar
    Dim pathRoot = sep & startingDirectory.Trim(New Char() {sep}) & sep
    Dim i = tidiedPath.IndexOf(pathRoot)

    If i < 0 Then
        Throw New DirectoryNotFoundException($"Cannot find {pathRoot} in {fullPath}.")
    End If

    ' There will be a DirectorySeparatorChar at the start - do not include it
    Return tidiedPath.Substring(i + 1)

End Function

So,

Dim s = "/C:/Dev/Edge/_Publications/Ann Report/2013-2016/2016 Edge.pdf"
Console.WriteLine(GetRelativePath(s, "_Publications"))
Console.WriteLine(GetRelativePath(s, "\Ann Report"))

outputs:

_Publications\Ann Report\2013-2016\2016 Edge.pdf
Ann Report\2013-2016\2016 Edge.pdf

Guessing that you might have several malformed paths starting with a "/" and using "/" as the directory separator character instead of "\", I put some code in to mitigate those problems.

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
-1

The Split() function is supposed to exclude the entire delimiter from the result. Could you re-check & confirm your input and output strings?

Chalky
  • 1,624
  • 18
  • 20
  • @chillzy is correct - the whole delimiter is removed, but providing a single string to the function means it is treated as an array of char delimiters. – Chalky Jan 27 '18 at 06:01