Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to count Enter in a string? [duplicate]

Possible Duplicate:
How would you count occurences of a string within a string (C#)?

I have a string that has multiple sub-strings and Enter (special character by pressing Enter key) between them.

Can you please guide me how to write a regular expression that counts Enter keys between words ?

Thanks

like image 464
user576510 Avatar asked Dec 01 '22 22:12

user576510


2 Answers

You don't need a regex, you're just counting strings. Specifically you're just counting Environment.Newlines. There's lots of ways to do that; several are described in this SO answer. Here's one which looks inefficient but performs surprisingly well:

int count1 = source.Length - source.Replace(Environment.Newline, "").Length;
like image 38
ean5533 Avatar answered Dec 05 '22 01:12

ean5533


Depending on the line break symbol used you may have to change to just \r or just \n.

var numberLineBreaks = Regex.Matches(input, @"\r\n").Count;
like image 146
Daniel Brückner Avatar answered Dec 05 '22 00:12

Daniel Brückner