-
Mar 5th, 2021, 03:04 PM
#1
Thread Starter
Fanatic Member
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!
-
Mar 5th, 2021, 03:28 PM
#2
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.
-
Mar 5th, 2021, 09:26 PM
#3
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|