Split Quoted String - CSharp System

CSharp examples for System:String Split

Description

Split Quoted String

Demo Code

// Permission is hereby granted, free of charge, to any person obtaining a
using System.IO;// ww w.ja  v a2s .  co  m
using System.Globalization;
using System.Collections.Generic;
using System;

public class Main{
        private static string[] SplitQuotedString(string source)
        {
            List<string> result = new List<string>();

            int idx = 0;
            while (idx < source.Length)
            {
                // Skip spaces
                while (source[idx] == ' ' && idx < source.Length)
                {
                    idx++;
                }

                if (source[idx] == '"')
                {
                    // A quoted value, find end of quotes...
                    int start = idx;
                    idx++;
                    while (idx < source.Length && source[idx] != '"')
                    {
                        idx++;
                    }

                    result.Add(source.Substring(start, idx - start + 1));
                }
                else
                {
                    // An unquoted value, find end of value
                    int start = idx;
                    idx++;
                    while (idx < source.Length && source[idx] != ' ')
                    {
                        idx++;
                    }

                    result.Add(source.Substring(start, idx - start));
                }

                idx++;
            }

            return result.ToArray();
        }
}

Related Tutorials