fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Diagnostics;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.IO;
  8. using System.Drawing;
  9. using System.Net;
  10.  
  11. namespace X
  12. {
  13. public static class ExtensionMethods
  14. {
  15. public static int Occurences(this string str, string val)
  16. {
  17. string copy = str;
  18.  
  19. int instancesOf = 0;
  20. int indexOfVal;
  21.  
  22. while ((indexOfVal = copy.IndexOf(val)) != -1)
  23. {
  24. copy = copy.Remove(indexOfVal, val.Count());
  25. instancesOf++;
  26. }
  27.  
  28. return instancesOf;
  29. }
  30. }
  31.  
  32. class Project
  33. {
  34. public static void Main()
  35. {
  36. string s = "ababab";
  37. Console.WriteLine(s.Occurences("ab"));
  38. Console.WriteLine(s.IndexOf("ba", 3, 3));
  39. }
  40. }
  41. }
Success #stdin #stdout 0.06s 24544KB
stdin
Standard input is empty
stdout
3
3