Split a string by semicolons while accounting for escaped characters

c#, connection-string, string

Solution

There is a DBConnectionStringBuilder class that will do what you want...

        System.Data.Common.DbConnectionStringBuilder builder = new System.Data.Common.DbConnectionStringBuilder();

        builder.ConnectionString = "Provider=\"Some;Provider\";Initial Catalog='Some;Catalog';";

        foreach (string key in builder.Keys)
        {
            Response.Write(String.Format("{0}: {1}<br>", key , builder[key]));
        }

Problem

Really simple problem: I want to split a connection string into its keyword / value pairs, so for example the following connection string: ``` Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=vm-jp-dev2;Data Source=scsql\sql2005;Auto Translate=False ``` Would become: ``` Provider=SQLOLEDB.1 Integrated Security=SSPI Persist Security Info=False Initial Catalog=vm-jp-dev2 Data Source=scsql\sql2005 Auto Translate=False ``` The trouble is that the MSDN documentation states that connection string values are allowed to contain semicolons if the value is enclosed in single or double quote characters, (so if I understand it the following would be valid): ``` Provider="Some;Provider";Initial Catalog='Some;Catalog';... ``` Whats the best way of splitting this string (in C#)?

Original source