How do I make a conditional if compared to only the last 2 digits?

5

I'm doing a project in Visual Basic but when the user types a string that will be 5 the maximum ex: UF052 I want to make a if where it will be compared.

If textbox1 = "UF052" the
  Comando
End if

But I do not want to compare all the characters and yes only 52 could be UG152 that still would fall in true .

    
asked by anonymous 11.04.2018 / 10:17

3 answers

4

You can compare the last two characters of your string without manipulating it and create a new string :

If(textBox1(textBox1.Length - 2) = "5"C And textBox1(textBox1.Length - 1) = "2"C)
    Comando
    
11.04.2018 / 12:07
2

Use Substring . Example:

Dim text As string = "Olá mundo!"
Dim teste As Boolean = text.Substring(text.Length - 2) = "o!"

Console.WriteLine(teste) //True
Console.WriteLine(text.Substring(text.Length - 2)) //o!

Better yet to use EndsWith

Dim text As string = "Olá mundo!"
Dim teste As Boolean = text.EndsWith("o!")

Console.WriteLine(teste) 
    
11.04.2018 / 10:29
2

Good afternoon, use "InStr" that will work as well. Here's an example:

if InStr("UF052", "52") > 0 then 
    Comando
End if

or if you just want to get the last two characters:

if InStr(Right("UF052", 2), "52") > 0 then 
    Comando
End if
    
11.04.2018 / 22:06