Login Register






Thread Rating:
  • 0 Vote(s) - 0 Average


Using Split() To Parse Data! filter_list
Author
Message
Using Split() To Parse Data! #1
The main reason why I'm doing this is because I see a lot of people using RegEx, when in testing Split() has been proven to function twice as fast (if you don't believe me, I'll show screenshots from a test I can set up). It can be used to get one piece of data, or a bunch of data on the page.

[code=vbnet]'Base code for all Split() usage
'Split(datasource, pattern)(index, usually 1).Split(end char)(index, always 0)
'~~
'Example 1 - Finding Numbers in Text
Dim src As String = "Unread 99, Total 199"
Dim unr As String = Split(src, "Unread ")(1).Split(",")(0) 'gets everything after Unread , and stops at the comma.
Dim tot As String = Split(src, "Total ")(1) 'since there is nothing after, no need to add the last bit.[/code]

The issue you may be noticing is that this example only covers splitting for one item, when you may have an undefined amount. This is where some people prefer regular expressions, however it is easily accomplished with Split.

[code=vbnet]Dim src As String = "(1000) (999) (888) (777) (666)"
Dim results() As String
Dim holder As New StringBuilder 'using String would cause a ton of temporary strings to be made, making this slow
Try
For i = 0 To Integer.MaxValue 'allows for hundreds of matches to be saved
holder.Append(Split(src, "(")(i).Split(")")(0) & "|")
Next
Catch ex As Exception 'An exception will occur when you run out of matches
results = holder.ToString.Split("|") 'you can optionally use SubString to remove the ending |.
End Try[/code]

Any issues, post below. Sorry if this isn't too clear, took me forever to write this on my tablet with the damn auto correct going off :p
[Image: CDUAq9d.png]

Reply

RE: Using Split() To Parse Data! #2
Pretty good tutorial.
I like it, its simple explained and good for beginners.
Smile

Reply

RE: Using Split() To Parse Data! #3
I like split simply for the fact that it is faster to code and not as complicated.

Reply

RE: Using Split() To Parse Data! #4
Great tutorial man, thanks for posting. :>

Reply







Users browsing this thread: 1 Guest(s)