![]() |
|
Using Split() To Parse Data! - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Coding (https://sinister.li/Forum-Coding) +--- Forum: Visual Basic & .NET Framework (https://sinister.li/Forum-Visual-Basic-NET-Framework) +--- Thread: Using Split() To Parse Data! (/Thread-Using-Split-To-Parse-Data) |
Using Split() To Parse Data! - Shebang - 08-14-2013 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 RE: Using Split() To Parse Data! - Madara-Uchiha - 08-14-2013 Pretty good tutorial. I like it, its simple explained and good for beginners.
RE: Using Split() To Parse Data! - Xanii - 08-14-2013 I like split simply for the fact that it is faster to code and not as complicated. RE: Using Split() To Parse Data! - ViolentReality - 08-14-2013 Great tutorial man, thanks for posting. :> |