Advertisement
dkeray

Count Substring in text

Jan 27th, 2013
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.77 KB | None | 0 0
  1. using System;
  2.  
  3. class Program
  4. {
  5.     public static int CountSubstring(string text, string subtr)
  6.     {
  7.         int count = 0;
  8.         text = text.ToLower();
  9.         while (text.IndexOf(subtr) != -1)
  10.         {
  11.             int pos = text.LastIndexOf(subtr);
  12.             if (text.Substring(pos, subtr.Length) != "")
  13.             {
  14.                 count++;
  15.                 text = text.Remove(pos);
  16.             }
  17.         }
  18.         return count;
  19.     }
  20.  
  21.     static void Main()
  22.     {
  23.         string text = "We are living in an yellow submarine. We don't have anything else. Inside the submarine is very tight. So we are drinking all the day. We will move out of it in 5 days.";
  24.         string substring = "in";
  25.         Console.WriteLine(CountSubstring(text, substring));
  26.     }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement