Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Count Char in String

Count Char in String

Scheduled Pinned Locked Moved C#
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bedevian
    wrote on last edited by
    #1

    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

    A 1 Reply Last reply
    0
    • B Bedevian

      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

      A Offline
      A Offline
      Alex Korchemniy
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups