123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace Utilities
- {
- public class QuotedStringUtils
- {
- public static string Quote(string str)
- {
- return String.Format("\"{0}\"", str);
- }
- public static string Unquote(string str)
- {
- string quote = '"'.ToString();
- if (str.Length >= 2 && str.StartsWith(quote) && str.EndsWith(quote))
- {
- return str.Substring(1, str.Length - 2);
- }
- else
- {
- return str;
- }
- }
- public static bool IsQuoted(string str)
- {
- string quote = '"'.ToString();
- if (str.Length >= 2 && str.StartsWith(quote) && str.EndsWith(quote))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- public static int IndexOfUnquotedChar(string str, char charToFind)
- {
- return IndexOfUnquotedChar(str, charToFind, 0);
- }
- public static int IndexOfUnquotedChar(string str, char charToFind, int startIndex)
- {
- if (startIndex >= str.Length)
- {
- return -1;
- }
- bool inQuote = false;
- int index = startIndex;
- while (index < str.Length)
- {
- if (str[index] == '"')
- {
- inQuote = !inQuote;
- }
- else if (!inQuote && str[index] == charToFind)
- {
- return index;
- }
- index++;
- }
- return -1;
- }
- public static int IndexOfUnquotedString(string str, string stringToFind)
- {
- return IndexOfUnquotedString(str, stringToFind, 0);
- }
- public static int IndexOfUnquotedString(string str, string stringToFind, int startIndex)
- {
- if (startIndex >= str.Length)
- {
- return -1;
- }
- bool inQuote = false;
- int index = startIndex;
- while (index < str.Length)
- {
- if (str[index] == '"')
- {
- inQuote = !inQuote;
- }
- else if (!inQuote && str.Substring(index).StartsWith(stringToFind))
- {
- return index;
- }
- index++;
- }
- return -1;
- }
- public static List<string> SplitIgnoreQuotedSeparators(string str, char separator)
- {
- List<string> result = new List<string>();
- int nextEntryIndex = 0;
- int separatorIndex = IndexOfUnquotedChar(str, separator);
- while (separatorIndex >= nextEntryIndex)
- {
- result.Add(str.Substring(nextEntryIndex, separatorIndex - nextEntryIndex));
- nextEntryIndex = separatorIndex + 1;
- separatorIndex = IndexOfUnquotedChar(str, separator, nextEntryIndex);
- }
- result.Add(str.Substring(nextEntryIndex));
- return result;
- }
- }
- }
|