Reputation: 685
I have a treeview where the tree nodes are generated from text in text files. The standard layout would be:-
jobname - jobnumber - jobtype (with the spaces in between the " - ")
For part of an operation I need to split this string into the 3 sections, for this I'm using LastIndexOf("-"c)
and using the `Microsoft.VisualBasic.Mid' function to split it out
Private Sub Job_Select_Load(sender As Object, e As EventArgs) Handles MyBase.Load
tbJobName.Text = Main.tvProgress.SelectedNode.Text
Dim enUK As New CultureInfo("en-GB")
Dim ToCheck As String = tbJobName.Text
Dim index1 As Integer = ToCheck.LastIndexOf("-"c)
Dim jCode As String = Microsoft.VisualBasic.Trim(Microsoft.VisualBasic.Mid(ToCheck, index1 - 5, 5))
Dim jOps As String = Microsoft.VisualBasic.Trim(Microsoft.VisualBasic.Mid(ToCheck, index1, 10))
The problem i have is when jobtype
also contains a '-' in the text as this then throws everything out. I've tried switching it around using IndexOf
however occasionally the jobname
will contain a '-'
I have no control over the text in jobname - jobnumber - jobtype
Any ideas on how to get round this
If you need any more info then I can update the post....
Upvotes: 0
Views: 77
Reputation: 1403
I'm assuming you don't have any control over how these files are formatted, because if you did, it would be much better to just change the file format to XML, or at least a CSV file format.
If there are an indefinite number of dashes in both the job name and type, then as long as the number is constant, you can use a Regular Expression to find the groups. It would look like this:
Dim name, number, type As String
Dim fileLine As String = "job - name - 1234 - job - type"
Dim regExp As New Regex("(?<name>.*)\s-\s(?<number>\d+)\s-\s(?<type>.*)")
Dim m As Match = regExp.Match(fileLine)
name = m.Groups("name").Value
number = m.Groups("number").Value
type = m.Groups("type").Value
Note: this only works if the "job number" is always a number. If not, you might be able to use a more complex regex to match it though.
Upvotes: 1
Reputation: 25023
If the separator between sections is space-dash-space then you can use the overload of String.Split which determines the maximum number of parts to split it in to:
Dim s = "job-name - jobnumber - jobtype (with the spaces in between the "" - "")"
Dim parts As String() = s.Split(New String() {" - "}, 3, StringSplitOptions.None)
Console.WriteLine("name: " & parts(0).Trim())
Console.WriteLine("number: " & parts(1).Trim())
Console.WriteLine("type: " & parts(2).Trim())
Outputs:
name: job-name
number: jobnumber
type: jobtype (with the spaces in between the " - ")
Upvotes: 0
Reputation: 1404
Could you not use Dim job As String() = Split(ToCheck, " - ")
? Note the spaces around the dash.
Upvotes: 0
Reputation: 44941
If jobname and jobnumber will never contain dashes, then you can just use cascading IndexOfs, with the last found position as the start of the next one.
Dim endOfJobName As Integer = ToCheck.IndexOf("-"c)
If endOfJobName <> - 1 Then
' Get jobname here if needed
Dim endOfJobNumber As Integer = ToCheck.IndexOf(endOfJobName, "-"c)
If endOfJobNumber <> -1 Then
' Get jobnumber here if needed
' The remainder of the string is jobtype
Dim jobType = ToCheck.SubString(endOfJobNumber + 1)
End If
End If
If either jobname or jobnumber can contain dashes, however, there is not a tremendous amount that can be done, other than changing the delimiter in the source.
Upvotes: 0