Question:
[VB .NET] Parse a string for values?
guylaroche
2007-11-02 09:08:58 UTC
** PROGRAMMING LANGUAGE IS VB .NET 2005 **


I have two strings:
1) $2000.00

2) 12/2/2007 8

I need help finding code to extract the $2000 part from string 1 and the digit 8 from string 2.

I have exhausted my brain thinking of ways to locate examples. I have looked into using regular expressions, the Split function, find and replace examples, parsing XML & HTML, etc. I cannot find anything which I can figure out how to use.

Any help is much appreciated.
Five answers:
programmer_va
2007-11-03 13:24:28 UTC
If you're trying to "scrape" the data from a website you would create a sub functions. First you need to add the whole set of html data to a string then loop through it. As you loop through you will need to use the indexof function to determine where the style="text-align: left"> begins such as:



'Start Position Of Price

Dim priceStartPosition as int = currentline.indexof("style="""text-align: left""">") + 27



'End Position Of Price

Dim priceEndPosition as int = currentline.substring(priceStartPosition,currentline.length).indexof("")



'Sets the characters between the start and end position to a variable called price

Dim price = currentline.substring(priceStartPosition ,currentline.length - priceEndPosition )



I've done exactly what you're trying to do but haven't touched the code in over 2 years.
Dave
2007-11-02 12:11:21 UTC
Since you have clarified that the example could be anything I guess you are really looking for getting the contents out of the middle of a span tag. If that is the case then since this is valid XML you should load your text as an XML document and then do a selectNodes command:



Here is some example code off the top of my head that will loop through all the span nodes in a valid xml document. This code is off the top of my head and not compiled so you will need to clean it up a bit



Dim s as string = "[your text]"

Dim xmlDoc as XmlDocument = new XmlDocument()

xmlDoc.loadXML(s)

if not nothing is xmlDoc then

dim nodes as XmlNodeList = xmlDoc.selectNodes("//span")

dim node as XmlNode

dim xmlText as string

for each node in nodes

xmlText = node.text

' know do whatever you want with the text inside the span node

next

end if
Brian G
2007-11-02 14:31:03 UTC
You should probably use the XmlTextReader, as it is extremely lightweight. Its a bit tricky to use with in-memory strings rather than documents. Here is some console application code I wrote to test:



Imports System.Xml

Imports System.IO

Module Module1

Sub Main()

Dim strSpan As String = "$2000.00"

Dim reader As New XmlTextReader(New MemoryStream(System. Text. Encoding. ASCII. GetBytes(strSpan)))

While reader.Read()

Dim strValue As String = reader. ReadElementContentAsString()

Console.WriteLine(strValue)

End While



reader.Close()



strSpan = "12/2/2007 8"

reader = New XmlTextReader(New MemoryStream(System. Text. Encoding. ASCII. GetBytes(strSpan)))

While reader.Read()

Dim strValue As String = reader. ReadElementContentAsString()

Console.WriteLine(strValue)

End While



Console.ReadLine()



reader.Close()

End Sub



End Module
vstar_in_texas
2007-11-02 11:58:52 UTC
In the simplest form,



You would do this to parse the strings



Dim string1 As String = "$2000.00"

Dim string2 As String = "12/2/2007 8"

Dim MoneyHolder As String

Dim dDateHolder As String



MoneyHolder = string1.Substring(0, 5).Trim

dDateHolder = string2.Substring(0, 9).Trim





MsgBox(MoneyHolder)

MsgBox(dDateHolder)
macharoni
2007-11-02 12:16:56 UTC
Dim str1 As String = "$2000.00"

Dim str2 As String = "12/2/2007 8"



' Get everything from index 1 to the end of the string

Dim strMoney As String = str1.Substring(1)

Dim dblMoney As Double = Convert.ToDouble(strMoney)

MessageBox.Show(dblMoney)



' Split the string into tokens using space as the delimeter

' then grab the second string

Dim strArray() As String = str2.Split(" ".ToCharArray())

Dim strDate As String = strArray(1)

MessageBox.Show(strDate)


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...