TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Count a Char Number of Times in a String
Ashok Paladugula
Dec 03, 2014
4.3
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
This blog shows how to count the number of occurrences of a char in a string.
C# Code
public
ActionResult GetRepeatedCharactorCount(
string
enterdString)
{
string
inputString = enterdString;
List<
string
> resultStringList =
new
List<
string
>();
char
[] chrs = inputString.ToCharArray();
for
(
int
i = 0; i < chrsList.Length; i++)
{
int
j = 0, count = 0;
while
((j = inputString.IndexOf(chrsList[i], j)) != -1)
{
count++;
j++;
}
var outputting =
string
.Format(
"{0} Number of times repeat : {1} in {2}"
, chrsList[i], count, inputString);
if
(resultStringList.IndexOf(outputString) == -1)
{
resultStringList.Add(outputString);
}
}
return
Json(
new
{ result = resultStringList}, JsonRequestBehavior.AllowGet);
}
Count a Char Number of Times in a String
Next Recommended Reading
Counting Characters In A Given String Using C#