Advertisement
RamarauntCodeNinja

String Extension Methods

Apr 16th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.66 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ValcourFiction.Utilities
  8. {
  9.     /// <summary>
  10.     /// Where all string extension methods are stored
  11.     /// </summary>
  12.     public static class StringExtension
  13.     {
  14.         #region Public Extension Methods
  15.         /// <summary>
  16.         /// Advanced seperator function for seperating strings by the given chars into an array (accessable)
  17.         /// </summary>
  18.         /// <param name="sep">The chars to seperate with (defaults to space only)</param>
  19.         /// <param name="septk">The chars to seperate and to add to the result (defaults to empty string)</param>
  20.         /// <returns>The delimited string array</returns>
  21.         public static string[] Delimit(this string str, string sep = " ", string septk = "")
  22.         {
  23.             List<string> ret = new List<string>();
  24.  
  25.             string tok = "";
  26.             for (int i = 0; i < str.Length; i++)
  27.             {
  28.                 if (sep.Contains(str[i]))
  29.                 {
  30.                     ret.Add(tok);
  31.                     tok = "";
  32.                 }
  33.                 else if (septk.Contains(str[i]))
  34.                 {
  35.                     ret.Add(str[i].ToString());
  36.                     ret.Add(tok);
  37.                     tok = "";
  38.                 }
  39.                 else
  40.                 {
  41.                     tok += str[i];
  42.                 }
  43.             }
  44.  
  45.             return ret.ToArray();
  46.         }
  47.  
  48.         /// <summary>
  49.         /// Advanced combinator function for combining a string array into a single string.
  50.         /// </summary>
  51.         /// <param name="sep">The seperator character</param>
  52.         /// <returns>The string array concatanated</returns>
  53.         public static string Combine(this string[] str, string sep = " ")
  54.         {
  55.             string ret = "";
  56.             for (int i = 0; i < str.Length; i ++)
  57.             {
  58.                 ret += str[i];
  59.                 if (i < str.Length - 1)
  60.                 {
  61.                     ret += sep;
  62.                 }
  63.             }
  64.             return ret;
  65.         }
  66.  
  67.         /// <summary>
  68.         /// Does the string contain the given char?
  69.         /// </summary>
  70.         /// <param name="c">The char to check for</param>
  71.         /// <returns>If the char was found</returns>
  72.         public static bool Contains(this string str, char c)
  73.         {
  74.             for (int i = 0; i < str.Length; i++)
  75.             {
  76.                 if (str[i] == c)
  77.                 {
  78.                     return true;
  79.                 }
  80.             }
  81.             return false;
  82.         }
  83.  
  84.         /// <summary>
  85.         /// Trim a string array of all strings in the given array (accessable)
  86.         /// </summary>
  87.         /// <param name="look4">The strings to trim</param>
  88.         /// <returns>The trimmed string array</returns>
  89.         public static string[] Trim(this string[] raw, string[] look4)
  90.         {
  91.             List<string> ret = raw.ToList<String>();
  92.  
  93.             for (int i = 0; i < ret.Count; i++)
  94.             {
  95.                 if (look4.Contains(ret[i]))
  96.                 {
  97.                     ret.RemoveAt(i);
  98.                     i--;
  99.                 }
  100.             }
  101.  
  102.             return ret.ToArray();
  103.         }
  104.  
  105.         /// <summary>
  106.         /// Does the given string array contain the given string? (accessable)
  107.         /// </summary>
  108.         /// <param name="str">The string to check for</param>
  109.         /// <returns>If raw contains str</returns>
  110.         public static bool Contains(this string[] raw, string str)
  111.         {
  112.             for (int i = 0; i < raw.Length; i++)
  113.             {
  114.                 if (raw[i] == str)
  115.                 {
  116.                     return true;
  117.                 }
  118.             }
  119.             return false;
  120.         }
  121.  
  122.         /// <summary>
  123.         /// Does the given string array contain the given string array of indices in order? (accessable)
  124.         /// </summary>
  125.         /// <param name="dat">The string array to check for</param>
  126.         /// <returns>If raw contains dat</returns>
  127.         public static bool Contains(this string[] raw, string[] dat)
  128.         {
  129.             for (int i = 0; i < raw.Length - dat.Length; i++)
  130.             {
  131.                 for (int inner = i; inner < i + dat.Length; inner++)
  132.                 {
  133.                     if (raw[inner] != dat[inner])
  134.                     {
  135.                         break;
  136.                     }
  137.                     else if (inner == i + dat.Length - 1)
  138.                     {
  139.                         return true;
  140.                     }
  141.                 }
  142.             }
  143.             return false;
  144.         }
  145.  
  146.         /// <summary>
  147.         /// Gets the index of the first occurance of the given string in the given array (-1 if not found) (accessable)
  148.         /// </summary>
  149.         /// <param name="str">The given string</param>
  150.         /// <returns>The index if it is found, -1 otherwise</returns>
  151.         public static int IndexOf(this string[] raw, string str)
  152.         {
  153.             for (int i = 0; i < raw.Length; i++)
  154.             {
  155.                 if (raw[i] == str)
  156.                 {
  157.                     return i;
  158.                 }
  159.             }
  160.  
  161.             return -1;
  162.         }
  163.  
  164.         /// <summary>
  165.         /// Gets the index of the first occurance of the given string array in the given string array (-1 if not found) (accessable)
  166.         /// </summary>
  167.         /// <param name="dat">The array to check for</param>
  168.         /// <returns>The first index found, or -1 if not found</returns>
  169.         public static int IndexOf(this string[] raw, string[] dat)
  170.         {
  171.             for (int i = 0; i < raw.Length - dat.Length; i++)
  172.             {
  173.                 for (int inner = i; inner < i + dat.Length; inner++)
  174.                 {
  175.                     if (raw[inner] != dat[inner])
  176.                     {
  177.                         break;
  178.                     }
  179.                     else if (inner == i + dat.Length - 1)
  180.                     {
  181.                         return i;
  182.                     }
  183.                 }
  184.             }
  185.             return -1;
  186.         }
  187.  
  188.         /// <summary>
  189.         /// Gets the indices of the given string array where the given string is found (accessable)
  190.         /// </summary>
  191.         /// <param name="str">The string to check for</param>
  192.         /// <returns>The resulting indices</returns>
  193.         public static int[] IndicesOf(this string[] raw, string str)
  194.         {
  195.             List<int> ret = new List<int>();
  196.             for (int i = 0; i < raw.Length; i++)
  197.             {
  198.                 if (raw[i] == str)
  199.                 {
  200.                     ret.Add(i);
  201.                 }
  202.             }
  203.             return ret.ToArray();
  204.         }
  205.  
  206.         /// <summary>
  207.         /// Gets the indices of the given string array where the given string array is found in order (accessable)
  208.         /// </summary>
  209.         /// <param name="dat">The array to check for</param>
  210.         /// <returns>The resulting indices</returns>
  211.         public static int[] IndicesOf(this string[] raw, string[] dat)
  212.         {
  213.             List<int> ret = new List<int>();
  214.             for (int i = 0; i < raw.Length - dat.Length; i++)
  215.             {
  216.                 for (int inner = i; inner < i + dat.Length; inner++)
  217.                 {
  218.                     if (raw[inner] != dat[inner])
  219.                     {
  220.                         break;
  221.                     }
  222.                     else if (inner == i + dat.Length - 1)
  223.                     {
  224.                         ret.Add(i);
  225.                     }
  226.                 }
  227.             }
  228.             return ret.ToArray();
  229.         }
  230.         #endregion
  231.     }
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement