How to know if a String contains any of the first 10 letters of the alphabet VB.Net

2

I have to do a program in vb.net to check if a string that is entered by keyboard contains any of the first 10 letters of the alphabet or not. I've tried this but it has not worked.

    Overloads Sub MostrarDatosCalculados(ByVal nombre As String, ByVal municipio As String)
    Dim Resultado As Boolean = nombre Like "[A-J]"
    Dim Resultado2 As Boolean = municipio Like "[A-J]"
    If Resultado = True Then
        Console.WriteLine("El nombre contiene una de las diez primeras letras del abecedario.")
    Else
        Console.WriteLine("El nombre NO contiene una de las diez primeras letras del abecedario")
    End If
    If Resultado2 = True Then
        Console.WriteLine("El municipio contiene una de las diez primeras letras del abecedario.")
    Else
        Console.WriteLine("El municipio NO contiene una de las diez primeras letras del abecedario")
    End If

End Sub
    
asked by Josu Ordóñez 02.10.2018 в 10:41
source

1 answer

1

Edited

After your clarification, it seems that what you want is to know if the string contains any of the first 10 characters. Solution with LINQ:

Dim letras As Char() = Enumerable.Range(Convert.ToInt32("a"c), Convert.ToInt32("j"c) - Convert.ToInt32("a"c) + 1).[Select](Function(c) Chr(c)).ToArray()
Dim algunaLetra = nombre.ToLower().Any(Function(x) letras.Contains(x))

We create a Char() with the characters from a to j , and then we check if there is a case where letras contains some letter of the input string

I leave my old solution since it can be interesting:

To check if a string contains the first 10 letters of the alphabet at least once:

Dim todasLasLetras = nombre.ToLower().Where(Function(x) x >= "a"c AndAlso x <= "j"c).Distinct().Count() = 10

Basically, we convert the name to lowercase, we get all the letters between the a and the j that are different, and if they are exactly 10, we have that the string has all the letters.

    
answered by 02.10.2018 / 10:57
source