search strings list

0

Good, someone could help me please or give an idea of how to solve the following problem

I have a string in the phrase "CASA", I need to go through the list of letters and form the word CASA is the string that is in the phrase list

    
asked by 03.02.2018 в 22:52
source

2 answers

1

To select from a list of letters the letters that exist in a string, you can use LINQ's Where extension method:

var frase = "CASA";

var letras = new List<string>
    {"C", "X", "A", "Y", "H", "S", "A"};
var letrasEncontradas = letras.Where(l => frase.Contains(l));

Debug.WriteLine(String.Join("", letrasEncontradas));

If you want to check if all the letters of frase are in letrasEncontradas you can also use LINQ:

var frase = "CASA";

var letras = new List<string>
    {"C", "X", "A", "Y", "H", "S", "A"};
// Selecciona las letras que están en frase
var letrasEncontradas = letras.Where(l => frase.Contains(l)).ToList();
// Comprueba si todas las letras de frase están en letrasEncontradas
bool todasEncontradas = frase.All(x => letrasEncontradas.Any(l => l == x.ToString()));

Debug.WriteLine(String.Join("", letrasEncontradas));
// Si no se han encontrado todas se muestra un mensaje indicándolo
if (!todasEncontradas)
{
    Debug.WriteLine($"Se encontraron {letrasEncontradas.Count()} letras, pero no se encontraron todas.");
}
    
answered by 03.02.2018 в 23:37
0

you could do something like this:

        
   string []stringToCheck = {"C","A","S","A"};
  string[] stringArray = { "W", "C", "A", "S","A" };
  foreach (string x in stringArray)
{
    if (stringToCheck.Contains(x))
    {
        // Process...
    }
}

Source of response: Stackoverflow in English

You can also see the find method of c #: here

    
answered by 03.02.2018 в 23:25