Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#Homework
Hi!
 
I am a beginner in c#.
My question is, how can I count the number of characters in a string?
Example: in some text i need to find certain letter (how many times is that letter is showing up) using foreach loop.
 
Thx
Posted 5-Apr-11 23:15pm
Edited 3 days ago
Rahul VB2.9K
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

int count = 0;
char charToCount = 'i';
foreach (char c in string123)
{
    if( c == charToCount )
    {
        count++;
    }
}
 
The above code will help you find the no. of instance of a particular character, here 'i', in the string, string123.
  Permalink  
v3
Comments
JOAT-MON at 6-Apr-11 5:54am
   
wrapped code block
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

try this
 
string abc="1234567777";
           int  k = abc.Length;
 
string abc = "adkjddaadtyy";
            int Count=0;
            foreach (char c in abc)
            {
                if (c == 'd')
                {
                    Count++;
                }
            }
            int k = Count;
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

If you're already using a foreach loop then you don't need, a priori, such a info.
e.g.:
String s = "foo abc";
int count = 0;
foreach (char c in s)
{
  count++;
}
  Permalink  
v2
Comments
JOAT-MON at 6-Apr-11 5:37am
   
I think OP is looking for frequency of a specific letter in the foreach, not just full count.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Why must you use a foreach loop? Since you're a beginner, here are some much more interesting solutions to look at!
 
string str = "abcdaab";
 
//Solution 1 - remove the character, and compare its length.
int result1 = str.Length - str.Replace("a", "").Length;
 
//Solution 2 - split the string into an array using the character as a delimiter
int result2 = str.Split('a').Length - 1;
 
//Solution 3 - use the LINQ 'Count' extension method
int result3 = str.ToCharArray().Count(c => c == 'a');
 
  Permalink  
v2
Comments
Kim Togo at 6-Apr-11 9:59am
   
My 5 for solution 3. Linq is a powerfull tool.
SAKryukov at 6-Apr-11 14:59pm
   
This one is good, especially link. Instead of "" you should better use string.Empty. Hard-coded (immediate) constants are bad, but for the codelet maybe acceptable. 5 anyway. --SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 515
1 OriginalGriff 498
2 Abhinav S 305
3 sanket saxena 295
4 thatraja 235
0 Sergey Alexandrovich Kryukov 8,482
1 OriginalGriff 4,830
2 Peter Leow 3,794
3 Maciej Los 3,515
4 Er. Puneet Goel 3,107


Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 13 Apr 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid