Load From String - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:ICollection

Description

Load From String

Demo Code


using System.Text;
using System.Collections.Generic;
using System;/*from  w  w w  .  ja va 2  s  . c  o m*/

public class Main{
        public static void LoadFromString(Dictionary<string, string> target, string value)
        {
            if (target.Count > 0)
            {
                throw new Exception("Target is not empty!");
            }
            if (string.IsNullOrEmpty(value))
            {
                return;
            }

            string[] xPairs = value.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var xPair in xPairs)
            {
                string[] xParts = xPair.Split('=');
                if (xParts.Length > 1)
                {
                    target.Add(xParts[0], xParts[1]);
                }
                else
                {
                    target.Add(xParts[0], "");
                }
            }
        }
}

Related Tutorials