Code Snippets

  

C# Source Code


Welcome to Dream.In.Code
Become a C# Expert!

Join 137,188 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 2,311 people online right now. Registration is fast and FREE... Join Now!





Count how many times a character is found in a string

Counts the number of times a character appears in a string.

Submitted By: aj32
Actions:
Rating:
Views: 2,668

Language: C#

Last Modified: May 22, 2008
Instructions: - Place the function in a class
- See example usage

Snippet


  1. /// <summary>
  2. /// Counts the number of times a character appears in a string.
  3. /// </summary>
  4. private string countchars(string str)
  5.         {
  6.             string[,] alphanumerics = { { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "a", "b", "c", "d", "e", "f", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" } };
  7.            
  8.             string result = "";
  9.  
  10.             int FI = 0;
  11.             int LI = 0;
  12.             int counts = 0;
  13.            
  14.  
  15.             for (int i = 0; i < 35; i++)
  16.             {
  17.  
  18.                 //Get the lower-case characters first//
  19.  
  20.                 FI = str.IndexOf(alphanumerics[0, i]);
  21.                 while (FI > -1)
  22.                 {
  23.                     counts++;
  24.                     LI = FI + 1;
  25.  
  26.                     FI = str.IndexOf(alphanumerics[0, i], LI);
  27.                 }
  28.  
  29.                 if (counts > 0)
  30.                 {
  31.                     result += "There are " + counts + " " + alphanumerics[0, i] + "'s.\n";
  32.                 }
  33.  
  34.                
  35.  
  36.                 //Count upper-case characters//
  37.  
  38.                 FI = str.IndexOf(alphanumerics[0, i].ToUpper());
  39.                 while (FI > -1)
  40.                 {
  41.                     counts++;
  42.                     LI = FI + 1;
  43.  
  44.                     FI = str.IndexOf(alphanumerics[0, i].ToUpper(), LI);
  45.                 }
  46.  
  47.                 if (counts > 0)
  48.                 {
  49.                     result += "There are " + counts + " " + alphanumerics[0, i].ToUpper() + "'s.\n";
  50.                 }
  51.  
  52.                 counts = 0;
  53.             }
  54.  
  55.            
  56.             return result;
  57.  
  58.         }
  59.  
  60.  
  61. //Example usage:
  62. //(After creating a textbox, named textBox1):
  63. MessageBox.Show(countchars(textBox1.Text));

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month