Can parse a string representing a string[] into an actual string[]. : String Split « Data Types « C# / C Sharp






Can parse a string representing a string[] into an actual string[].

    

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RestCake.Util
{
  public static class StringUtil
  {
    /// <summary>
    /// Can parse a string representing a string[] into an actual string[].
    /// You can delimit strings with nothing (though commas can't be in the string values), ' or ".
    /// Surrounding [] chars are optional.
    /// Examples:
    ///    a,b,c
    ///    'a','b','c'
    ///    "a","b","c"
    ///    [a,b,c]
    ///    ['a','b','c']
    ///    ["a","b","c"]
    /// A space after a comma is ok, but if you aren't using a string delimiter, a space will be added to the next string's beginning (" b").
    /// Escaped characters also work, such as ["foo, \"bar\"", "\"quoted string\"", "'single quoted with \" delimiter'"]
    /// You can't mix delimiters.  Use all nothing, all ', or all ".  The first delimiter found will be used (at char index 0 or 1, depending on the wrapping []s presence)
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public static string[] ParseStringArray(string input)
    {
      if (String.IsNullOrWhiteSpace(input))
        return null;
      
      input = input.Trim();
      
      bool brackets = input[0] == '[' && input[input.Length - 1] == ']';
      
      // String delimeter can be ' or ", or nothing.
      // Note that without a string delimeter, you can't have commas in your string, cause that's what we'll split on.
      char delim = 'X';
      // Depending on if we're brackets in []s or not, we'll look for the string delimiter at index 0 or 1
      int delimIndex = brackets ? 1 : 0;
      
      if (input[delimIndex] == '\'')
        delim = '\'';
      else if (input[delimIndex] == '"')
        delim = '"';
        
      //Console.WriteLine("input: " + input);
      //Console.WriteLine("delim: " + delim);
      //Console.WriteLine("brackets: " + brackets);
      
      if (delim == 'X')
      {
        // easiest case, split on ,
        if (brackets)
          // Get rid of the wrapping []'s
          return input.Substring(1, input.Length - 2).Split(',');
        else
          return input.Split(',');
      }
      // From here on, we KNOW we have a string delimiter of ' or "
      
      bool inString = false;
      bool escapeNext = false;
      StringBuilder sb = new StringBuilder();
      List<string> strings = new List<string>();
      
      for(int i = 0; i < input.Length; ++i)
      {
        // Skip any possible wrapping [] chars
        if (brackets && (i == 0 || i == input.Length - 1))
          continue;

        char c = input[i];

        if (escapeNext)
        {
          //Console.WriteLine("escaping char: " + c);
          escapeNext = false;
          sb.Append(c);
        }
        else if (c == '\\' && inString)
        {
          //Console.WriteLine("Will escape next");
          escapeNext = true;
          sb.Append(c);
        }
        else if (c == delim)
        {
          //Console.WriteLine("at delim");
          inString = !inString;
        }
        else if (c == ',')
        {
          Console.Write("at comma: ");
          if (inString)
          {
            //Console.WriteLine("instring");
            // This is a comma in the string
            sb.Append(c);
          }
          else
          {
            //Console.WriteLine("out of string");
            // This comma separates one string from another
            //Console.WriteLine("end string");
            strings.Add(sb.ToString());
            sb.Clear();
          }
        }
        else if (inString)
        {
          sb.Append(c);
        }
      }
      // The last string wasn't added, because it gets added when a comma is encountered, and there's no last comma
      strings.Add(sb.ToString());
      
      return strings.ToArray();
    }
  }
}

   
    
    
    
  








Related examples in the same category

1.Split with space
2.Split with a list of dividers
3.Split spaces with space
4.Show string in proper case
5.use the Split() method to split strings
6.Splitting StringsSplitting Strings
7.Split and join stringsSplit and join strings
8.Tokenize stringsTokenize strings
9.Char String foreachChar String foreach
10.Split a string delimited by another string and return 2 non-empty elements
11.Split a string delimited by another string and return 2 elements
12.Split the original string at the delimiter and return all non-empty elements
13.Split a string delimited by another string and return all elements
14.Split a string delimited by characters and return all elements
15.Split a string delimited by characters and return 2 non-empty elements
16.Split a string delimited by characters and return 2 elements
17.Split a string delimited by characters and return all non-empty elements
18.Use StringSplitOptions enumeration to include or exclude substrings generated by the Split method.
19.Implements a StringTokenizer class for splitting a string into substrings using a set of delimiters.
20.String Tokenizer
21.Converts a collection of tags to a single string representation containing the tags separated by a comma
22.Takes in any string and convert it into a Byte array, suitable for e.g. insertion into a REG_BINARY Registry value.
23.Splits the string into lines.
24.Splits the string into words (all white space is removed).
25.Split Quoted String
26.Helper class to split a long word into a single one.
27.Splits the string into an array, using the separator.
28.Splits string name into a readable string based on camel casing.