Advertisement
AlFas

Extensions

Jun 26th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.92 KB | None | 0 0
  1. public static class Extensions
  2. {
  3.     public static int Find(this string s, string match)
  4.     {
  5.         for (int i = 0; i <= s.Length - match.Length; i++)
  6.         {
  7.             string sub = s.Substring(i, match.Length);
  8.             if (sub == match)
  9.                 return i;
  10.         }
  11.         return -1;
  12.     }
  13.     public static int Find(this string s, string match, int occurence)
  14.     {
  15.         int occurences = 0;
  16.         for (int i = 0; i <= s.Length - match.Length; i++)
  17.         {
  18.             string sub = s.Substring(i, match.Length);
  19.             if (sub == match)
  20.             {
  21.                 occurences++;
  22.                 if (occurences == occurence)
  23.                     return i;
  24.             }
  25.         }
  26.         return -1;
  27.     }
  28.     public static int Find(this string s, string match, int start, int end)
  29.     {
  30.         for (int i = start; i <= end - match.Length; i++)
  31.         {
  32.             string sub = s.Substring(i, match.Length);
  33.             if (sub == match)
  34.                 return i;
  35.         }
  36.         return -1;
  37.     }
  38.     public static int GetLastNumber(this string s)
  39.     {
  40.         int n = 0;
  41.         bool isLastNumber = false;
  42.         for (int i = 0; i < s.Length; i++)
  43.         {
  44.             isLastNumber = int.TryParse(s.Substring(i), out n);
  45.             if (isLastNumber)
  46.                 return n;
  47.         }
  48.         throw new ArgumentException("The string has no number in the end.");
  49.     }
  50.     public static int[] FindAll(this string s, string match)
  51.     {
  52.         List<int> indices = new List<int>();
  53.         for (int i = 0; i <= s.Length - match.Length; i++)
  54.         {
  55.             string sub = s.Substring(i, match.Length);
  56.             if (sub == match)
  57.                 indices.Add(i);
  58.         }
  59.         return indices.ToArray();
  60.     }
  61.     public static int[] FindAll(this string s, string match, int start, int end)
  62.     {
  63.         List<int> indices = new List<int>();
  64.         for (int i = start; i <= end - match.Length; i++)
  65.         {
  66.             string sub = s.Substring(i, match.Length);
  67.             if (sub == match)
  68.                 indices.Add(i);
  69.         }
  70.         return indices.ToArray();
  71.     }
  72.     public static string Substring(this string s, string from, string to)
  73.     {
  74.         int startIndex = s.Find(from) + from.Length;
  75.         int endIndex = s.Find(to);
  76.         int length = endIndex - startIndex;
  77.         return s.Substring(startIndex, length);
  78.     }
  79.     public static string RemoveLastNumber(this string s)
  80.     {
  81.         string l = s;
  82.         for (int i = 0; i < s.Length; i++)
  83.         {
  84.             l = s.Substring(i);
  85.             if (int.TryParse(l, out int shit))
  86.                 return s.Substring(0, i);
  87.         }
  88.         return s;
  89.     }
  90.     public static string Replace(this string originalString, string stringToReplaceWith, int startIndex, int length)
  91.     {
  92.         string result = originalString;
  93.         result = result.Remove(startIndex, length);
  94.         result = result.Insert(startIndex, stringToReplaceWith);
  95.         return result;
  96.     }
  97.     public static string ReplaceWholeWord(this string originalString, string oldString, string newString)
  98.     {
  99.         for (int i = originalString.Length - oldString.Length; i >= 0; i--)
  100.         {
  101.             if (originalString.Substring(i, oldString.Length) == oldString)
  102.                 if ((i > 0 ? (!originalString[i - 1].IsLetterOrNumber()) : true) && (i < originalString.Length - oldString.Length ? (!originalString[i + oldString.Length].IsLetterOrNumber()) : true))
  103.                 {
  104.                     originalString = originalString.Replace(newString, i, oldString.Length);
  105.                     i -= oldString.Length;
  106.                 }
  107.         }
  108.         return originalString;
  109.     }
  110.     public static int[] FindOccurences(this string[] a, string match)
  111.     {
  112.         if (a != null)
  113.         {
  114.             List<int> occurences = new List<int>();
  115.             for (int i = 0; i < a.Length; i++)
  116.                 if (a[i] == match)
  117.                     occurences.Add(i);
  118.             return occurences.ToArray();
  119.         }
  120.         else return new int[0];
  121.     }
  122.     public static bool ContainsWholeWord(this string s, string match)
  123.     {
  124.         for (int i = s.Length - match.Length; i >= 0; i--)
  125.         {
  126.             if (s.Substring(i, match.Length) == match)
  127.                 if ((i > 0 ? (!s[i - 1].IsLetterOrNumber()) : true) && (i < s.Length - match.Length ? (!s[i + match.Length].IsLetterOrNumber()) : true))
  128.                     return true;
  129.         }
  130.         return false;
  131.     }
  132.     public static bool Contains(this string[] a, string match)
  133.     {
  134.         if (a != null)
  135.         {
  136.             for (int i = 0; i < a.Length; i++)
  137.                 if (a[i] == match)
  138.                     return true;
  139.             return false;
  140.         }
  141.         return false;
  142.     }
  143.     public static bool ContainsAtLeast(this string[] a, string match)
  144.     {
  145.         if (a != null)
  146.         {
  147.             for (int i = 0; i < a.Length; i++)
  148.                 if (a[i].Contains(match))
  149.                     return true;
  150.             return false;
  151.         }
  152.         return false;
  153.     }
  154.     public static bool ContainsAtLeastWholeWord(this string[] a, string match)
  155.     {
  156.         if (a != null)
  157.         {
  158.             for (int i = 0; i < a.Length; i++)
  159.                 if (a[i].ContainsWholeWord(match))
  160.                     return true;
  161.             return false;
  162.         }
  163.         return false;
  164.     }
  165.     public static string[] Replace(this string[] a, char oldChar, char newChar)
  166.     {
  167.         for (int i = 0; i < a.Length; i++)
  168.             a[i] = a[i].Replace(oldChar, newChar);
  169.         return a;
  170.     }
  171.     public static string[] Replace(this string[] a, string oldString, string newString)
  172.     {
  173.         for (int i = 0; i < a.Length; i++)
  174.             a[i] = a[i].Replace(oldString, newString);
  175.         return a;
  176.     }
  177.     public static string[] ReplaceWholeWord(this string[] a, string oldString, string newString)
  178.     {
  179.         for (int i = 0; i < a.Length; i++)
  180.             a[i] = a[i].ReplaceWholeWord(oldString, newString);
  181.         return a;
  182.     }
  183.     public static string Combine(this string[] s)
  184.     {
  185.         StringBuilder str = new StringBuilder();
  186.         for (int i = 0; i < s.Length; i++)
  187.             str = str.Append(s[i]);
  188.         return str.ToString();
  189.     }
  190.     public static string Combine(this string[] s, string separator)
  191.     {
  192.         StringBuilder str = new StringBuilder();
  193.         for (int i = 0; i < s.Length; i++)
  194.             str = str.Append(s[i] + separator);
  195.         return str.ToString();
  196.     }
  197.     public static bool IsNumber(this char c)
  198.     {
  199.         if (c >= 48 && c <= 57)
  200.             return true;
  201.         return false;
  202.     }
  203.     public static bool IsLetter(this char c)
  204.     {
  205.         if (c >= 65 && c <= 90 || c >= 97 && c <= 122)
  206.             return true;
  207.         return false;
  208.     }
  209.     public static bool IsLetterOrNumber(this char c) => IsLetter(c) || IsNumber(c) ;
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement