Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ count character apperance

Tags:

c#

linq

Instead of looping over my string I'd like to use LINQ. How to do the following?

//  explode our word
List<char> rackBag = new List<char>();
rackBag.AddRange("MYWORD??".ToCharArray());

// How many wildcards?
int wildCardCount = rackBag.Count(x => x.Equals("?"));

wildCardCount should equal 2.

like image 236
ryan Avatar asked Oct 05 '10 19:10

ryan


2 Answers

Lots of unneeded steps there. Try this:

int wildCardCount = "MYWORD??".Count(x => x == '?');
like image 147
jball Avatar answered Oct 02 '22 16:10

jball


rackBag.Count(x => x == '?'); 
like image 36
Viv Avatar answered Oct 02 '22 17:10

Viv