Testing for repeated characters in a string

后端 未结 11 824
闹比i
闹比i 2020-12-16 22:55

I\'m doing some work with strings, and I have a scenario where I need to determine if a string (usually a small one < 10 characters) contains repeated characters.

11条回答
  •  情话喂你
    2020-12-16 23:50

    How about something like:

    string strString = "AA BRA KA DABRA";
    
    var grp = from c in strString.ToCharArray() 
            group c by c into m
            select new { Key = m.Key, Count = m.Count() };
    
    foreach (var item in grp)
    {
        Console.WriteLine(
            string.Format("Character:{0} Appears {1} times", 
            item.Key.ToString(), item.Count));
    }
    

提交回复
热议问题