c# check if “*” is being repeated in a string

  • A+
Category:Languages

I'm trying to count how many * symbols i have in my string. however im getting an error.

An unhandled exception of type 'System.ArgumentException' occurred in System.dll

I'm simply using a regex match to check it. When i tested with any other string it worked perfectly, but when i search for " * " it get's an exception.

Here's the code that gives expresion

string abc = "i am just trying *** for a sample code"; var count = Regex.Matches(abc, "*").Count; Console.Out.WriteLine(count); 

This one works perfectly

string abc = "i am just trying  for a sample code"; var count = Regex.Matches(abc, "a").Count; Console.Out.WriteLine(count); 

Any Idea why ?


Why don't use LINQ Count istead:

string abc = "i am just trying *** for a sample code"; var result = abc.Count(c=>c=='*'); 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: