How to use the split with more data in visual

1

I have a question, I would like to know how to separate my variables and numbers from the operators, here is my example:

Dim letras As String()
    Dim texto As String = "abc*5+x/(y+z)"
    letras = texto.Split(CChar("*"))
    For i = 0 To letras.Count - 1
        MsgBox(letras(i))
    Next

letras(0)=abc
letras(1)=5+x/(y+z)

My question is how could I separate it for each operator I find and something like this would remain:

letras(0)=abc
letras(1)=5
letras(2)=x
letras(3)=y
letras(4)=z

What could be the best way to do it, thanks

    
asked by ismae 05.03.2018 в 01:26
source

1 answer

1

As well @ gbianchi says in a comment, it seems that what you want to do is a tree of expressions to analyze a mathematical equation. That is a complex issue that escapes the scope of this site. But I am going to give you a solution to simply divide a string using several separators. For this, String.Split in one of its overloads admits a Array of separator characters, as well as an option to eliminate the empty chains that result from the division of the chain. By using them, you can achieve the result you expect in your example:

Dim texto As String = "abc*5+x/(y+z)"
Dim separadores As Char() = New [Char]() {"+"c, "-"c, "*"c, "/"c, "("c, ")"c}
Dim letras As String() = texto.Split(separadores, StringSplitOptions.RemoveEmptyEntries)

This returns in letters the result you expect:

letras(0)=abc
letras(1)=5
letras(2)=x
letras(3)=y
letras(4)=z
    
answered by 05.03.2018 / 09:22
source