4

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

Community
  • 1
  • 1
user576510
  • 5,777
  • 20
  • 81
  • 144
  • 4
    Why do you need a _regular expression_ for counting the number of occurrences of a character in a string? That's overkill. Just count the characters some other way. Iterate through the characters. Or perhaps there is a library function. – Ray Toal Dec 13 '12 at 03:25
  • Would the "Enter"s be `"\r\n"` (`CRLF`), `"\n"` (`LF`) or `"\r"` (`CR`)? – Alvin Wong Dec 13 '12 at 03:26
  • 3
    "Hey, I have a problem that is related to strings. I must need a regex." – ean5533 Dec 13 '12 at 03:26
  • 2
    Lots of ways here: http://stackoverflow.com/questions/541954/how-would-you-count-occurences-of-a-string-within-a-string-c – ean5533 Dec 13 '12 at 03:28

5 Answers5

9

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;
Daniel Brückner
  • 59,031
  • 16
  • 99
  • 143
7

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;
Community
  • 1
  • 1
ean5533
  • 8,884
  • 3
  • 40
  • 64
3

Does it have to be a regular expression? There are probably easier ways... for example, you could use string[] array = String.Split('\n'); to create an array of the substrings, then get the count with array.Length;

Tim Long
  • 13,508
  • 19
  • 79
  • 147
0

You could do by simply counting the newlines:

int start = -1;
int count = 0;
while ((start = text.IndexOf(Environment.NewLine, start + 1)) != -1)
    count++;
return count;
neeKo
  • 4,280
  • 23
  • 31
0

You can use this Code,

using System;
using System.Text.RegularExpressions;

class Program
    {
        static void Main()
        {
         long a = CountLinesInString("This is an\r\nawesome website.");
         Console.WriteLine(a);

         long b = CountLinesInStringSlow("This is an awesome\r\nwebsite.\r\nYeah.");
         Console.WriteLine(b);
         }

         static long CountLinesInString(string s)
         {
          long count = 1;
          int start = 0;
          while ((start = s.IndexOf('\n', start)) != -1)
          {
              count++;
               start++;
          } 
          return count;
         }

         static long CountLinesInStringSlow(string s)
         {
          Regex r = new Regex("\n", RegexOptions.Multiline);
          MatchCollection mc = r.Matches(s);
          return mc.Count + 1;
         }
 }
Morteza Azizi
  • 479
  • 5
  • 23