Count Char in String
-
i have a string "13255ad27d" this string it not fixed it might be "1212221121" but i know that the lenth is fixed = 10 wonna count how many 0,1,2.....9 and A,B,C...F if string is "1212221121" A=0 B=0 1=5 2=5 3=0 4=0 do this from 0-9 and A-f
Here is a nice quick way of doing this:
string text = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Mauris ut dui vitae nulla eleifend malesuada. Aliquam semper tortor nec arcu. Donec consequat tincidunt ipsum. Praesent tempor magna nec massa. Curabitur ut tortor. Fusce massa. Pellentesque vulputate, ante quis mattis euismod, magna elit pretium enim, eu varius nisl eros vel nisl. Pellentesque quis enim. Phasellus eu erat. Ut convallis pede quis purus. Aliquam nec diam ut mauris congue suscipit. Nam eleifend congue sapien. Cras vulputate erat id ligula. Suspendisse potenti. Sed laoreet aliquet lorem.";
// Count
Hashtable charCounter = new Hashtable();
foreach(char c in text.ToCharArray())
{
if (charCounter.ContainsKey(c))
{
charCounter[c] = (int)charCounter[c] + 1;
}
else
{
charCounter.Add(c, 0);
}
}
// Test
foreach(char c in charCounter.Keys)
{
System.Diagnostics.Trace.WriteLine("Char: " + c + " " + charCounter[c]);
}This posting is provided "AS IS" with no warranties, and confers no rights. Alex Korchemniy