QuotedStringUtils.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace Utilities
  5. {
  6. public class QuotedStringUtils
  7. {
  8. public static string Quote(string str)
  9. {
  10. return String.Format("\"{0}\"", str);
  11. }
  12. public static string Unquote(string str)
  13. {
  14. string quote = '"'.ToString();
  15. if (str.Length >= 2 && str.StartsWith(quote) && str.EndsWith(quote))
  16. {
  17. return str.Substring(1, str.Length - 2);
  18. }
  19. else
  20. {
  21. return str;
  22. }
  23. }
  24. public static bool IsQuoted(string str)
  25. {
  26. string quote = '"'.ToString();
  27. if (str.Length >= 2 && str.StartsWith(quote) && str.EndsWith(quote))
  28. {
  29. return true;
  30. }
  31. else
  32. {
  33. return false;
  34. }
  35. }
  36. public static int IndexOfUnquotedChar(string str, char charToFind)
  37. {
  38. return IndexOfUnquotedChar(str, charToFind, 0);
  39. }
  40. public static int IndexOfUnquotedChar(string str, char charToFind, int startIndex)
  41. {
  42. if (startIndex >= str.Length)
  43. {
  44. return -1;
  45. }
  46. bool inQuote = false;
  47. int index = startIndex;
  48. while (index < str.Length)
  49. {
  50. if (str[index] == '"')
  51. {
  52. inQuote = !inQuote;
  53. }
  54. else if (!inQuote && str[index] == charToFind)
  55. {
  56. return index;
  57. }
  58. index++;
  59. }
  60. return -1;
  61. }
  62. public static int IndexOfUnquotedString(string str, string stringToFind)
  63. {
  64. return IndexOfUnquotedString(str, stringToFind, 0);
  65. }
  66. public static int IndexOfUnquotedString(string str, string stringToFind, int startIndex)
  67. {
  68. if (startIndex >= str.Length)
  69. {
  70. return -1;
  71. }
  72. bool inQuote = false;
  73. int index = startIndex;
  74. while (index < str.Length)
  75. {
  76. if (str[index] == '"')
  77. {
  78. inQuote = !inQuote;
  79. }
  80. else if (!inQuote && str.Substring(index).StartsWith(stringToFind))
  81. {
  82. return index;
  83. }
  84. index++;
  85. }
  86. return -1;
  87. }
  88. public static List<string> SplitIgnoreQuotedSeparators(string str, char separator)
  89. {
  90. return SplitIgnoreQuotedSeparators(str, separator, StringSplitOptions.None);
  91. }
  92. public static List<string> SplitIgnoreQuotedSeparators(string str, char separator, StringSplitOptions options)
  93. {
  94. List<string> result = new List<string>();
  95. int nextEntryIndex = 0;
  96. int separatorIndex = IndexOfUnquotedChar(str, separator);
  97. while (separatorIndex >= nextEntryIndex)
  98. {
  99. string entry = str.Substring(nextEntryIndex, separatorIndex - nextEntryIndex);
  100. if (options != StringSplitOptions.RemoveEmptyEntries || entry != String.Empty)
  101. {
  102. result.Add(entry);
  103. }
  104. nextEntryIndex = separatorIndex + 1;
  105. separatorIndex = IndexOfUnquotedChar(str, separator, nextEntryIndex);
  106. }
  107. string lastEntry = str.Substring(nextEntryIndex);
  108. if (options != StringSplitOptions.RemoveEmptyEntries || lastEntry != String.Empty)
  109. {
  110. result.Add(lastEntry);
  111. }
  112. return result;
  113. }
  114. }
  115. }