1

I am trying to divide a string similar to this:

"<span>1</span> - Selection"

And get the value that is enclosed between the <span>.

In JavaScript I do this:

var example= "<span>1</span> - selection";

var separation= example.replace("</span>","<span>").split("<span>");
console.log("Split: ",separation);
//The result is ["","1"," - Selección"]
console.log("Value that I want: ",separation[1]); //I get the value that I need

And that's it that I need to do but in Visual .NET, but it doesn't work for me.

I try:

Dim WordString As String = "<span>1</span> - Selection"
Dim idSelection As String() = WordString.Replace("</span>","<span>").Split("<span>")

or sending all the </span> replaced in the string to just do:

Dim WordString As String = "<span>1<span> - Selection"
Dim idSelection As String() = WordString.Split("<span>")

But in the position (1) I always get "span>1", and I can't do the split like in JS

How can I do it correctly?


To simulate the code VB.Net use https://dotnetfiddle.net/

The code:

Imports System

Public Module Module1
    Public Sub Main()
        Dim WordString As String = "<span>1</span> - Selection"
        Dim idSelection As String() = WordString.Replace("</span>","<span>").Split("<span>")
        Console.WriteLine(idSelection(1))
    End Sub
End Module
Fabian Montoya
  • 407
  • 4
  • 19
  • Use an HTML parser would be best, another option is `regex` try this: `(.|\n)*?<\/span>`. Again, there's more than a few ways to actually accomplish this. – Trevor Oct 21 '19 at 20:55
  • I cannot reproduce your problem. The first example works as in JS – Steve Oct 21 '19 at 20:58
  • @Steve use https://dotnetfiddle.net/ and paste the code, only add `Console.WriteLine(idSelection(1))` in the End – Fabian Montoya Oct 21 '19 at 21:06
  • @Çöđěxěŕ I know that it's html, but the question isn't only for HTML, is for a String that y want divide by other string, do you know? – Fabian Montoya Oct 21 '19 at 21:07
  • @FabianMontoya Yes, I do know how. I left a comment above about it, did you read it? – Trevor Oct 21 '19 at 21:12

1 Answers1

1

You have to use Split(String[], StringSplitOptions) to split using a string. So you can use the following solution:

Imports System

Public Module Module1
    Public Sub Main()
        Dim WordString As String = "<span>1</span> - Selection"
        Dim idSelection As String() = WordString.Replace("</span>","<span>").Split({"<span>"}, StringSplitOptions.RemoveEmptyEntries)
        Console.WriteLine(idSelection(0))
    End Sub
End Module

demo on dotnetfiddle.net

You can also use a solution using a regular expression with a positiv lookahead and lookbehind:

Imports System
Imports System.Text.RegularExpressions

Public Module Module1
    Public Sub Main()
        Dim rgx As New Regex("(?<=<span>)(.+)(?=</span>)")
        Dim WordString As String = "<span>1</span> - Selection"

        If rgx.IsMatch(WordString) Then
            Console.WriteLine(rgx.Matches(WordString)(0))
        End If  
    End Sub
End Module

demo on dotnetfiddle.net

Sebastian Brosch
  • 42,106
  • 15
  • 72
  • 87