开发者

To read all characters from a string entered in text box without duplicates and count of each character

I want to read all characters from a string entered in a text box without repetitio开发者_C百科ns with the count of each character and then store these values to two grid columns using C# ,Asp.Net

eg:My name is Joe

Characters  Occurances
M               2
y               1
n               1
a               1
e               2
i               1
s               1
j               1
o               1
Total           11

and then store these to the grid view columns


You can use LINQ operator GroupBy:

string str = ":My name is Joe";

var result = str.Where(c => char.IsLetter(c)).
                 GroupBy(c => char.ToLower(c)).
                 Select(gr => new { Character = gr.Key, Count = gr.Count()}).
                 ToList();

That'll give you a list of objects with fields Character and Count.

EDIT: I added a Where clause to filter out non-letter characters. Also added case-insensitivity

Now you have to use the result list as a binding source for your grid. My understanding of ASP.NET binding process is a little rusty. It is possible that you'll need to create objects of some class with Character and Count properties instead of anonymous objects (you can't bind to fields)


string input = "My name is Joe";

var result = from c in input.ToCharArray()
             where !Char.IsWhiteSpace(c)
             group c by Char.ToLower(c)
             into g
             select new Tuple<string, int>(g.Key.ToString(),g.Count());

int total = result.Select(o => o.Item2).Aggregate((i, j) => i + j);

List<Tuple<string, int>> tuples = result.ToList();

tuples.Add(new Tuple<string, int>("Total", total));

And you can databind the tuples list to the DataGrid :)


If you don't like the LINQ solutions, here's how it can be done using foreach-loops:

string str = ":My name is Joe";

var letterDictionary = new Dictionary<char,int>();

foreach (char c in str) {
    // Filtering non-letter characters
    if (!char.IsLetter(c))
        continue;

    // We use lowercase letter as a comparison key
    // so "M" and "m" are considered the same characters
    char key = char.ToLower(c);

    // Now we try to get the number of times we met
    // this key already.
    // TryGetValue method will only affect charCount variable
    // if there is already a dictionary entry with this key,
    // otherwise its value will be set to default (zero)
    int charCount;
    letterDictionary.TryGetValue(key, out charCount);

    // Either way, we now have to increment the charCount value
    // for our character and put it into dictionary
    letterDictionary[key] = charCount + 1;  
}

foreach (var kvp in letterDictionary) {
    Console.WriteLine("{0}: {1}", kvp.Key, kvp.Value);
}

The output of the last cycle will be:

m: 2
y: 1
n: 1
a: 1
e: 2
i: 1
s: 1
j: 1
o: 1

Now you have to use some technique from other answers to turn the result into the list of value that can be bound to the datagrid.


I have tried simple console application..hope this will help you.. you can view this on Solving Algorithms with C#

To read all characters from a string entered in text box without duplicates and count of each character

Here is the result:

To read all characters from a string entered in text box without duplicates and count of each character

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜