jhenriquecosta

removeprefix

Oct 28th, 2021
666
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  /// <summary>
  2.         /// Removes first occurrence of the given prefixes from beginning of the given string.
  3.         /// Ordering is important. If one of the preFixes is matched, others will not be tested.
  4.         /// </summary>
  5.         /// <param name="str">The string.</param>
  6.         /// <param name="preFixes">one or more prefix.</param>
  7.         /// <returns>Modified string or the same string if it has not any of given prefixes</returns>
  8.         public static string RemovePreFix(this string str, params string[] preFixes)
  9.         {
  10.             if (str == null)
  11.             {
  12.                 return null;
  13.             }
  14.  
  15.             if (string.IsNullOrEmpty(str))
  16.             {
  17.                 return string.Empty;
  18.             }
  19.  
  20.             if (preFixes.IsNullOrEmpty())
  21.             {
  22.                 return str;
  23.             }
  24.  
  25.             foreach (var preFix in preFixes)
  26.             {
  27.                 if (str.StartsWith(preFix))
  28.                 {
  29.                     return str.Right(str.Length - preFix.Length);
  30.                 }
  31.             }
  32.  
  33.             return str;
  34.         }
RAW Paste Data