Get two values from a string

1

I'm creating a programming language and for you to define variables in it you use this code:

def NomeDaVariavel = ValorDela;

And I wanted to know how I get only the Nome da variável field, separated from ValorDela .

Any ideas? Thank you.

Update

If (Regex.IsMatch(currentText, "^\s*(def)")) Then
   Dim tempProvider As Match = Regex.Match(currentText, "^\s*def.*[^=]=")
   Dim nomeDaVariavel As String = tempProvider.Value.Substring(
              currentText.IndexOf("def") + Iff(currentText.Trim() = " = ", 4, 3)).Replace("=", "").Replace(" ", "")
   Dim valorDaVariavel As String = ParseStr(currentText)
   MsgBox(FX("A Variável '
def testando = 'Olá, mundo!';
' tem o valor de ''.", nomeDaVariavel, valorDaVariavel)) Continue For End If

This worked because I tested it like this:

def NomeDaVariavel = ValorDela;

And it worked! The tip is ;-)

    
asked by anonymous 10.05.2015 / 22:19

1 answer

1

You can use String.Substring method . to extract some of the text.

Dim texto As String = "def NomeDaVariavel = ValorDela;"

Dim indiceVar As Integer = texto.IndexOf("def")
Dim indiceVal As Integer = texto.IndexOf("=")

Dim tamanhoVar As Integer = Len(indiceVar)

Dim varNome As String = texto.Substring(indiceVar + tamanhoVar, texto.IndexOf("=") - tamanhoVar - 1)
Dim varVal As String = texto.Substring(indiceVal + 2, texto.IndexOf(";") - indiceVal - 2)

Console.WriteLine(String.Format("{0} = {1}", varNome, varVal))
Console.ReadKey()

If you need to get each word, use the String.Split :

Dim texto As String = "def NomeDaVariavel = ValorDela;"

Dim palavras As String() = texto.Split(New Char() {" "})
Dim palavra As String
For Each palavra In palavras
    Console.WriteLine(palavra)
Next
Console.ReadKey()

Exemplo

    
10.05.2015 / 22:35