Keine Beschreibung
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

StringExtensions.cs 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System;
  2. using System.Text.RegularExpressions;
  3. namespace UnityEditor.Rendering
  4. {
  5. /// <summary>
  6. /// Set of utility functions with <see cref="string"/>
  7. /// </summary>
  8. public static class StringExtensions
  9. {
  10. private static Regex k_InvalidRegEx = new (string.Format(@"([{0}]*\.+$)|([{0}]+)", Regex.Escape(new string(System.IO.Path.GetInvalidFileNameChars()))), RegexOptions.Compiled);
  11. /// <summary>
  12. /// Replaces invalid characters for a filename or a directory with a given optional replacemenet string
  13. /// </summary>
  14. /// <param name="input">The input filename or directory</param>
  15. /// <param name="replacement">The replacement</param>
  16. /// <returns>The string with the invalid characters replaced</returns>
  17. public static string ReplaceInvalidFileNameCharacters(this string input, string replacement = "_") => k_InvalidRegEx.Replace(input, replacement);
  18. /// <summary>
  19. /// Checks if the given string ends with the given extension
  20. /// </summary>
  21. /// <param name="input">The input string</param>
  22. /// <param name="extension">The extension</param>
  23. /// <returns>True if the extension is found on the string path</returns>
  24. public static bool HasExtension(this string input, string extension) =>
  25. input.EndsWith(extension, StringComparison.OrdinalIgnoreCase);
  26. /// <summary>
  27. /// Checks if a string contains any of the strings given in strings to check and early out if it does
  28. /// </summary>
  29. /// <param name="input">The input string</param>
  30. /// <param name="stringsToCheck">List of strings to check</param>
  31. /// <returns>True if the input contains any of the strings from stringsToCheck; otherwise, false.</returns>
  32. public static bool ContainsAny(this string input, params string[] stringsToCheck)
  33. {
  34. if(string.IsNullOrEmpty(input))
  35. return false;
  36. foreach (var value in stringsToCheck)
  37. {
  38. if(string.IsNullOrEmpty(value))
  39. continue;
  40. if (input.Contains(value))
  41. return true;
  42. }
  43. return false;
  44. }
  45. }
  46. }