Rakesh Rajan's blog

Thoughts on .NET, software and a few trivial things...

Find out the number of times a character is repeated in a string without using iteration

Problem

You want to find out the number of times a specific character is repeated in a string, but you don't want to use iterative blocks like while, for etc..

Solution

Use the Split method, and subtract 1 from the length of the string returned.

Example


	string input = "123#345#456#";
	int count = input.Split("#").Length - 1; // Returns 3

Posted: Apr 14 2005, 11:24 AM by rakeshrajan | with 4 comment(s)
Filed under:

Comments

TrackBack said:

Find out the number of times a character is repeated in a string without using iterationooeess
# May 24, 2005 2:12 AM

TrackBack said:

Find out the number of times a character is repeated in a string without using iterationooeess
# July 22, 2005 3:28 PM

rakeshrajan said:

Bright idea!
# July 22, 2005 5:32 PM

rakeshrajan said:

Thanks!

- Rakesh Rajan
# July 22, 2005 8:21 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)