Count characters in string C#

Hi, how do I count characters in a string with C#?

more ▼

asked Oct 30, 2012 at 05:55 PM

Kayden007 gravatar image

29 18 20 23

Unity Answers is for Unity specific questions only. Please ask language specific questions on a programming forum. Also a 2 second Google search could have answered this question for you.

Oct 30, 2012 at 06:07 PM Meltdown

I did try google since that's the first thing I always do, and this is for unity 3D(thought I mentioned that, guess I left it out)

Oct 30, 2012 at 06:10 PM Kayden007

C# is not specific to Unity. It is C#. Unity uses the MONO.NET framework which is almost identical to the Microsoft.NET framework.

Oct 30, 2012 at 06:24 PM Meltdown
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first


more ▼

answered Oct 30, 2012 at 05:56 PM

XienDev gravatar image

453 24 25 31

May I ask how to implement it? Or an example please.

Oct 30, 2012 at 05:59 PM Kayden007

Kayden, I'd suggest doing this C# video course below, your questions are absolute basics of programming and it seems you are not ready to even ask questions on a forum.


Oct 30, 2012 at 06:08 PM Meltdown

Example: string value = "abcde"; int x = value.Length;

And, what Meltdown said. If you hope to make a game, you must learn to program.

Oct 30, 2012 at 06:38 PM Dave Carlile

wow... this thread was brutal... :)

Oct 30, 2012 at 08:40 PM DannyB
(comments are locked)
10|3000 characters needed characters left

Your millage may vary and this can be done a lot of ways and Length is the right choice, but here you go.

 using System;
 public class SillyStringQuestion
   private string sentence;
   public SillyStringQuestion(string theString)
     this.sentence = theString;
   public int CountCharsTheString()
     int result = 0;
       charArray = sentence.ToCharArray();
       foreach(char character in charArray)
       throw new Exception("bummer no data and stuff");
     return result;
more ▼

answered Oct 30, 2012 at 06:53 PM

Landern gravatar image

9.6k 1 6 41

(comments are locked)
10|3000 characters needed characters left

For those that are interested, this website has benchmarked at least a dozen different ways to count the number of times a character occurs in a string, using both single threaded and multithreaded techniques.

Basically, the winner is either the single or parallel for-loop implementations as follows where "ss" is the Search String, "ch" is the array of characters you're looking for.:

 for (int x = 0; x < ss.Length; x++)
     for (int y = 0; y < ch.Length; y++)
         for (int a = 0; a < ss[x].Length; a++ )
         if (ss[x][a] == ch[y])
             //it's a match. Do what you need to.

more ▼

answered Apr 15 at 01:05 AM

firemyst gravatar image

15 1

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Oct 30, 2012 at 05:55 PM

Seen: 4742 times

Last Updated: Jul 05 at 12:52 PM