Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static class Extensions
- {
- public static int Find(this string s, string match)
- {
- for (int i = 0; i <= s.Length - match.Length; i++)
- {
- string sub = s.Substring(i, match.Length);
- if (sub == match)
- return i;
- }
- return -1;
- }
- public static int Find(this string s, string match, int occurence)
- {
- int occurences = 0;
- for (int i = 0; i <= s.Length - match.Length; i++)
- {
- string sub = s.Substring(i, match.Length);
- if (sub == match)
- {
- occurences++;
- if (occurences == occurence)
- return i;
- }
- }
- return -1;
- }
- public static int Find(this string s, string match, int start, int end)
- {
- for (int i = start; i <= end - match.Length; i++)
- {
- string sub = s.Substring(i, match.Length);
- if (sub == match)
- return i;
- }
- return -1;
- }
- public static int GetLastNumber(this string s)
- {
- int n = 0;
- bool isLastNumber = false;
- for (int i = 0; i < s.Length; i++)
- {
- isLastNumber = int.TryParse(s.Substring(i), out n);
- if (isLastNumber)
- return n;
- }
- throw new ArgumentException("The string has no number in the end.");
- }
- public static int[] FindAll(this string s, string match)
- {
- List<int> indices = new List<int>();
- for (int i = 0; i <= s.Length - match.Length; i++)
- {
- string sub = s.Substring(i, match.Length);
- if (sub == match)
- indices.Add(i);
- }
- return indices.ToArray();
- }
- public static int[] FindAll(this string s, string match, int start, int end)
- {
- List<int> indices = new List<int>();
- for (int i = start; i <= end - match.Length; i++)
- {
- string sub = s.Substring(i, match.Length);
- if (sub == match)
- indices.Add(i);
- }
- return indices.ToArray();
- }
- public static string Substring(this string s, string from, string to)
- {
- int startIndex = s.Find(from) + from.Length;
- int endIndex = s.Find(to);
- int length = endIndex - startIndex;
- return s.Substring(startIndex, length);
- }
- public static string RemoveLastNumber(this string s)
- {
- string l = s;
- for (int i = 0; i < s.Length; i++)
- {
- l = s.Substring(i);
- if (int.TryParse(l, out int shit))
- return s.Substring(0, i);
- }
- return s;
- }
- public static string Replace(this string originalString, string stringToReplaceWith, int startIndex, int length)
- {
- string result = originalString;
- result = result.Remove(startIndex, length);
- result = result.Insert(startIndex, stringToReplaceWith);
- return result;
- }
- public static string ReplaceWholeWord(this string originalString, string oldString, string newString)
- {
- for (int i = originalString.Length - oldString.Length; i >= 0; i--)
- {
- if (originalString.Substring(i, oldString.Length) == oldString)
- if ((i > 0 ? (!originalString[i - 1].IsLetterOrNumber()) : true) && (i < originalString.Length - oldString.Length ? (!originalString[i + oldString.Length].IsLetterOrNumber()) : true))
- {
- originalString = originalString.Replace(newString, i, oldString.Length);
- i -= oldString.Length;
- }
- }
- return originalString;
- }
- public static int[] FindOccurences(this string[] a, string match)
- {
- if (a != null)
- {
- List<int> occurences = new List<int>();
- for (int i = 0; i < a.Length; i++)
- if (a[i] == match)
- occurences.Add(i);
- return occurences.ToArray();
- }
- else return new int[0];
- }
- public static bool ContainsWholeWord(this string s, string match)
- {
- for (int i = s.Length - match.Length; i >= 0; i--)
- {
- if (s.Substring(i, match.Length) == match)
- if ((i > 0 ? (!s[i - 1].IsLetterOrNumber()) : true) && (i < s.Length - match.Length ? (!s[i + match.Length].IsLetterOrNumber()) : true))
- return true;
- }
- return false;
- }
- public static bool Contains(this string[] a, string match)
- {
- if (a != null)
- {
- for (int i = 0; i < a.Length; i++)
- if (a[i] == match)
- return true;
- return false;
- }
- return false;
- }
- public static bool ContainsAtLeast(this string[] a, string match)
- {
- if (a != null)
- {
- for (int i = 0; i < a.Length; i++)
- if (a[i].Contains(match))
- return true;
- return false;
- }
- return false;
- }
- public static bool ContainsAtLeastWholeWord(this string[] a, string match)
- {
- if (a != null)
- {
- for (int i = 0; i < a.Length; i++)
- if (a[i].ContainsWholeWord(match))
- return true;
- return false;
- }
- return false;
- }
- public static string[] Replace(this string[] a, char oldChar, char newChar)
- {
- for (int i = 0; i < a.Length; i++)
- a[i] = a[i].Replace(oldChar, newChar);
- return a;
- }
- public static string[] Replace(this string[] a, string oldString, string newString)
- {
- for (int i = 0; i < a.Length; i++)
- a[i] = a[i].Replace(oldString, newString);
- return a;
- }
- public static string[] ReplaceWholeWord(this string[] a, string oldString, string newString)
- {
- for (int i = 0; i < a.Length; i++)
- a[i] = a[i].ReplaceWholeWord(oldString, newString);
- return a;
- }
- public static string Combine(this string[] s)
- {
- StringBuilder str = new StringBuilder();
- for (int i = 0; i < s.Length; i++)
- str = str.Append(s[i]);
- return str.ToString();
- }
- public static string Combine(this string[] s, string separator)
- {
- StringBuilder str = new StringBuilder();
- for (int i = 0; i < s.Length; i++)
- str = str.Append(s[i] + separator);
- return str.ToString();
- }
- public static bool IsNumber(this char c)
- {
- if (c >= 48 && c <= 57)
- return true;
- return false;
- }
- public static bool IsLetter(this char c)
- {
- if (c >= 65 && c <= 90 || c >= 97 && c <= 122)
- return true;
- return false;
- }
- public static bool IsLetterOrNumber(this char c) => IsLetter(c) || IsNumber(c) ;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement