".Contains" in Conditions. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

".Contains" in Conditions.

Como puedo realizar un "else" correctamente cuando implemento un método ".Contains" de una matriz que contiene varios elementos, en un "if" dentro de un bucle. Por favor, necesito ayuda.

19th May 2022, 8:32 PM
Andrey
2 Answers
+ 2
No need to perform else part, just take a variable like 'count' with 0 and increment this count each time when condition met. Then outside the loop check if count is still 0, if yes then print "no match" No es necesario realizar otra parte, simplemente tome una variable como 'recuento' e incremente este conteo cada vez que se cumpla la condición. Luego, fuera del ciclo, verifique si el conteo sigue siendo 0, si es así, imprima "sin coincidencia" int count = 0; foreach (string w in words) { if(w.Contains(letter)) { Console.WriteLine(w); count++; } } if (count == 0) Console.WriteLine("No match");
20th May 2022, 3:18 AM
A͢J
A͢J - avatar
0
Muchas gracias, te lo agradezco mucho.
20th May 2022, 5:26 AM
Andrey