ExtensionString.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. public static class ExtensionString
  5. {
  6. public static T ToEnum<T>(this string str)
  7. {
  8. return (T)Enum.Parse(typeof(T), str);
  9. }
  10. public static string Remove(this string str, int startIndex, int endIndex, bool empty)
  11. {
  12. if (startIndex > endIndex)
  13. {
  14. throw new Exception();
  15. }
  16. return str.Remove(startIndex, endIndex - startIndex + 1);
  17. }
  18. public static string Replace(this string str, int startIndex, int endIndex, string newStr)
  19. {
  20. if (startIndex > endIndex)
  21. {
  22. throw new Exception();
  23. }
  24. str = str.Remove(startIndex, endIndex - startIndex + 1);
  25. str = str.Insert(startIndex, newStr);
  26. return str;
  27. }
  28. public static string Between(this string str, int startIndex, int endIndex)
  29. {
  30. if (startIndex > endIndex)
  31. {
  32. return "";
  33. }
  34. else if (startIndex == endIndex)
  35. {
  36. return str[startIndex].ToString();
  37. }
  38. else
  39. {
  40. return str.Substring(startIndex, endIndex - startIndex + 1);
  41. }
  42. }
  43. }