Results 1 to 3 of 3

Thread: String.Split Function

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2013
    Location
    Minneapolis, MN
    Posts
    531

    String.Split Function

    Hello:

    Having another insane moment today, where the last 5 minutes of the work takes all day.

    _filedata is: "C:\Sackett-Waconia\Migration Workgroup\Workgroup\CONVEYOR\BT220\CURVE SECTION\Details\E7153765.slddrw | SHEET 10 GA, AISI 304, TOP SHEET"

    FilePath is: "C:\Sackett-Waconia\Migration"

    FilePath should be: "C:\Sackett-Waconia\Migration Workgroup\Workgroup\CONVEYOR\BT220\CURVE SECTION\Details\E7153765.slddrw"

    My code is here:

    Code:
        Private Function GetPathFromFileData(ByVal _filedata As String) As String
            Debug.WriteLine(_filedata)
            ' C:\Sackett-Waconia\Migration Workgroup\Workgroup\CONVEYOR\BT220\CURVE SECTION\Details\E7153765.slddrw | SHEET 10 GA, AISI 304, TOP SHEET
    
            Dim farr() As String
            farr = _filedata.Split(" | ")
    
            Dim FilePath As String = farr(0)
            Debug.WriteLine(FilePath)
            ' C:\Sackett-Waconia\Migration
    
            Return FilePath
    
        End Function

    Thank you.
    - A 'Hyperactive Member' trying to make a difference in a hyperactive world! And recently, I've been promoted to a 'Finatic Member,' whatever that means!

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: String.Split Function

    The String.Split method has a number of overloads, see documentation: https://docs.microsoft.com/en-us/dot...m.string.split

    Because there is no overload method for Split(String), it is using Split(Char[]) because a String is a collection of Char values.

    You should be using the Split(String[], StringSplitOptions):
    Code:
    Private Function GetPathFromFileData(ByVal _filedata As String) As String
        Dim farr = _filedata.Split({" | "}, StringSplitOptions.None)
        Return farr(0)
    End Function
    Fiddle: https://dotnetfiddle.net/ynbCVK

    Edit - alternatively you could still use the Split(Char[]) method if you passed just the pipe and then trimmed the results:
    Code:
    Private Function GetPathFromFileData(ByVal _filedata As String) As String
        Dim farr = _filedata.Split("|"c)
        Return farr(0).Trim()
    End Function
    Fiddle: https://dotnetfiddle.net/uA3biF
    Last edited by dday9; Mar 5th, 2021 at 03:31 PM.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: String.Split Function

    You should immediately turn Option Strict On in your project. It would have alerted you that you were not doing what you thought you were doing by refusing to compile, rather than allowing the issue to slip through to run time and produce unexpected behaviour. How many other issues are lurking in your code because you didn't use the correct data type? You should also turn Option Strict On in the VS options, so it is On by default for all future projects.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width