0

This is driving me crazy. texto.Count() without the lambda "works", it returns the same value as texto.Length and then porcentaje == 100 in all cases. But as soon as i put the lambda in there: texto.Count(x => x == c) it returns 0, so porcentaje == 0 every time. It just make no sense, if you read the code its obvious that texto contains c. texto is NOT empty and iam using DECIMAL for porcentaje. This happens with any string i pass as the parameter. Any idea of what is going on???

public static List<string[]> Estadistica (string texto)
{
    //Devuelve la estadistica de aparicion de cada caracter del texto

    string caracteresUnicos = new String(texto.Distinct().ToArray());

    List<string[]> porcentajeCaracter = new List<string[]>();

    foreach (char c in caracteresUnicos)
    {
        decimal porcentaje = texto.Count(x => x == c) / texto.Length * 100;
        porcentajeCaracter.Add(new string[] { c.ToString(), porcentaje.ToString() });
    }

    porcentajeCaracter = porcentajeCaracter.OrderByDescending(arr => arr[1]).ToList();
    return porcentajeCaracter;
}
  • Side note: there is no `String.Count()`... Presumably you are talking about `Enumerable.Count()`... – Alexei Levenkov Mar 07 '20 at 06:07
  • You may want to consult with author of https://stackoverflow.com/questions/60574183/string-count-dosent-work-returns-0-in-a-not-empty-string-no-exception as they asked *very similar* question... If it happens to be you you are doing posting here wrong... Ideally you'd create [MCVE] to demonstrate the problem - remove all that unnecessary string code and have just couple values stored in variables... – Alexei Levenkov Mar 07 '20 at 06:16

0 Answers0