StringExtension.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. namespace textUtility
  2. {
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. public static class StringExtension
  8. {
  9. public static bool IsNullOrEmpty(this string str)
  10. {
  11. return string.IsNullOrEmpty(str);
  12. }
  13. public static string Unwrap(this string str)
  14. {
  15. int leftIndex = str.IndexOf('(') + 1;
  16. int rightIndex = str.LastIndexOf(')') - 1;
  17. return str.Substring(leftIndex, rightIndex - leftIndex + 1);
  18. }
  19. public static string Replace(this string str, int index, int length, string newStr)
  20. {
  21. str = str.Remove(index, length);
  22. str = str.Insert(index, newStr);
  23. return str;
  24. }
  25. public static string Between(this string str, int startIndex, int endIndex)
  26. {
  27. if (startIndex > endIndex)
  28. {
  29. throw new Exception();
  30. }
  31. else if (startIndex == endIndex)
  32. {
  33. return str[startIndex].ToString();
  34. }
  35. else
  36. {
  37. return str.Substring(startIndex, endIndex - startIndex + 1);
  38. }
  39. }
  40. public static List<string> ReplaceAll(this List<string> strings, string oldValue, string newValue)
  41. {
  42. List<string> results = new List<string>();
  43. for (int i = 0; i < strings.Count; i++)
  44. {
  45. results.Add(strings[i].Replace(oldValue, newValue));
  46. }
  47. return results;
  48. }
  49. }
  50. }