Advertisement
ivandrofly

GetLineNumber

Jan 7th, 2015
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.10 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. namespace Testing_Console
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             // Part 1
  10.             var watch  = new System.Diagnostics.Stopwatch();
  11.             var length = 0;
  12.             watch.Start();
  13.             length = GetLineLength1("- Ivandro Ismael\r\n- Gomes Jao"); // Split
  14.             watch.Stop();
  15.             Console.WriteLine(watch.Elapsed + " " + length);
  16.             watch.Reset();
  17.             watch.Start();
  18.             length = GetLineLength2("- Ivandro Ismael\r\n- Gomes Jao"); // Replace
  19.             watch.Stop();
  20.             Console.WriteLine(watch.Elapsed + " " + length);
  21.             Console.ReadLine();
  22.         }
  23.         static char[] newLineChars = Environment.NewLine.ToArray();
  24.         static int GetLineLength1(string line)
  25.         {
  26.             return line.Split(newLineChars, StringSplitOptions.RemoveEmptyEntries).Length;
  27.         }
  28.  
  29.         static int GetLineLength2(string line)
  30.         {
  31.             return line.Length - line.Replace(Environment.NewLine, string.Empty).Length;
  32.         }
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement