Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ValcourFiction.Utilities
- {
- /// <summary>
- /// Where all string extension methods are stored
- /// </summary>
- public static class StringExtension
- {
- #region Public Extension Methods
- /// <summary>
- /// Advanced seperator function for seperating strings by the given chars into an array (accessable)
- /// </summary>
- /// <param name="sep">The chars to seperate with (defaults to space only)</param>
- /// <param name="septk">The chars to seperate and to add to the result (defaults to empty string)</param>
- /// <returns>The delimited string array</returns>
- public static string[] Delimit(this string str, string sep = " ", string septk = "")
- {
- List<string> ret = new List<string>();
- string tok = "";
- for (int i = 0; i < str.Length; i++)
- {
- if (sep.Contains(str[i]))
- {
- ret.Add(tok);
- tok = "";
- }
- else if (septk.Contains(str[i]))
- {
- ret.Add(str[i].ToString());
- ret.Add(tok);
- tok = "";
- }
- else
- {
- tok += str[i];
- }
- }
- return ret.ToArray();
- }
- /// <summary>
- /// Advanced combinator function for combining a string array into a single string.
- /// </summary>
- /// <param name="sep">The seperator character</param>
- /// <returns>The string array concatanated</returns>
- public static string Combine(this string[] str, string sep = " ")
- {
- string ret = "";
- for (int i = 0; i < str.Length; i ++)
- {
- ret += str[i];
- if (i < str.Length - 1)
- {
- ret += sep;
- }
- }
- return ret;
- }
- /// <summary>
- /// Does the string contain the given char?
- /// </summary>
- /// <param name="c">The char to check for</param>
- /// <returns>If the char was found</returns>
- public static bool Contains(this string str, char c)
- {
- for (int i = 0; i < str.Length; i++)
- {
- if (str[i] == c)
- {
- return true;
- }
- }
- return false;
- }
- /// <summary>
- /// Trim a string array of all strings in the given array (accessable)
- /// </summary>
- /// <param name="look4">The strings to trim</param>
- /// <returns>The trimmed string array</returns>
- public static string[] Trim(this string[] raw, string[] look4)
- {
- List<string> ret = raw.ToList<String>();
- for (int i = 0; i < ret.Count; i++)
- {
- if (look4.Contains(ret[i]))
- {
- ret.RemoveAt(i);
- i--;
- }
- }
- return ret.ToArray();
- }
- /// <summary>
- /// Does the given string array contain the given string? (accessable)
- /// </summary>
- /// <param name="str">The string to check for</param>
- /// <returns>If raw contains str</returns>
- public static bool Contains(this string[] raw, string str)
- {
- for (int i = 0; i < raw.Length; i++)
- {
- if (raw[i] == str)
- {
- return true;
- }
- }
- return false;
- }
- /// <summary>
- /// Does the given string array contain the given string array of indices in order? (accessable)
- /// </summary>
- /// <param name="dat">The string array to check for</param>
- /// <returns>If raw contains dat</returns>
- public static bool Contains(this string[] raw, string[] dat)
- {
- for (int i = 0; i < raw.Length - dat.Length; i++)
- {
- for (int inner = i; inner < i + dat.Length; inner++)
- {
- if (raw[inner] != dat[inner])
- {
- break;
- }
- else if (inner == i + dat.Length - 1)
- {
- return true;
- }
- }
- }
- return false;
- }
- /// <summary>
- /// Gets the index of the first occurance of the given string in the given array (-1 if not found) (accessable)
- /// </summary>
- /// <param name="str">The given string</param>
- /// <returns>The index if it is found, -1 otherwise</returns>
- public static int IndexOf(this string[] raw, string str)
- {
- for (int i = 0; i < raw.Length; i++)
- {
- if (raw[i] == str)
- {
- return i;
- }
- }
- return -1;
- }
- /// <summary>
- /// Gets the index of the first occurance of the given string array in the given string array (-1 if not found) (accessable)
- /// </summary>
- /// <param name="dat">The array to check for</param>
- /// <returns>The first index found, or -1 if not found</returns>
- public static int IndexOf(this string[] raw, string[] dat)
- {
- for (int i = 0; i < raw.Length - dat.Length; i++)
- {
- for (int inner = i; inner < i + dat.Length; inner++)
- {
- if (raw[inner] != dat[inner])
- {
- break;
- }
- else if (inner == i + dat.Length - 1)
- {
- return i;
- }
- }
- }
- return -1;
- }
- /// <summary>
- /// Gets the indices of the given string array where the given string is found (accessable)
- /// </summary>
- /// <param name="str">The string to check for</param>
- /// <returns>The resulting indices</returns>
- public static int[] IndicesOf(this string[] raw, string str)
- {
- List<int> ret = new List<int>();
- for (int i = 0; i < raw.Length; i++)
- {
- if (raw[i] == str)
- {
- ret.Add(i);
- }
- }
- return ret.ToArray();
- }
- /// <summary>
- /// Gets the indices of the given string array where the given string array is found in order (accessable)
- /// </summary>
- /// <param name="dat">The array to check for</param>
- /// <returns>The resulting indices</returns>
- public static int[] IndicesOf(this string[] raw, string[] dat)
- {
- List<int> ret = new List<int>();
- for (int i = 0; i < raw.Length - dat.Length; i++)
- {
- for (int inner = i; inner < i + dat.Length; inner++)
- {
- if (raw[inner] != dat[inner])
- {
- break;
- }
- else if (inner == i + dat.Length - 1)
- {
- ret.Add(i);
- }
- }
- }
- return ret.ToArray();
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement