Split text to get a specific value

How can I split this xml node to get only the value of a number?

<span>All Potatoes: 4</span>

Something like that:

Dim code as string = "<span>All Potatoes: 4</span>"
Dim splitrsult as string
Splitresult = splitresult("<span>All Potatoes:" & "</span>")
Msgbox(splitresult)

I am new to this language and help would be appreciated. Thanks!

+4
source share
3 answers

Treat XML as XML! (and not as a simple string)

Use this import statement

Imports System.Xml.Linq

Then parse your string to get the Xml element and get its value

Dim code As String = "<span>All Potatoes: 4</span>"
Dim node = XElement.Parse(code)
Dim result As String = node.Value ' ==> "All Potatoes: 4"
+3
source

As another answer is mentioned, use an XML parser to get the value between the tags. Once this is done, if your text definitely has a format description : <value> , the most efficient way would be to split the line

Dim description As String = node.Value
Dim value As Integer = Integer.Parse(description.Split(": ")(1).Trim())

, : -, Integer.TryParse. , :,

Dim a() Ad String = description.Split(":")
Dim val As Integer 
If Not Integer.TryParse(a(a.Length - 1), val) Then
    MessageBox.Show("Value is not found")
Else
    MessageBox.Show("Value is " & val.ToString())
End If

Regex . , Regex

+2

To get the value of a number using Regex: (It's simple. Never be afraid of Regex)

Dim code as string = "<span>All Potatoes: 4</span>"
resultString = Regex.Match(code, @"\d+").Value
+2
source

Source: https://habr.com/ru/post/1613031/


All Articles