Use C# to split comma separated strings outside quotes
For parameter strings containing embedded commas outside quotes, you can use regular expression methods to split them exactly.
Question:
Split the string "('ABCDEFG', 123542, 'XYZ 99,9')" into its component parts, where the last argument contains a comma within quotes.
Solution:
Use regular expressions to identify commas outside quotes to achieve the desired split:
<code class="language-c#">",(?=(?:[^']*'[^']*')*[^']*$)"</code>
This regex pattern matches any comma that does not appear before or after an odd number of quotes.
Implementation:
The following code uses regular expressions to split a sample string:
<code class="language-c#">string samplestring = "('ABCDEFG', 123542, 'XYZ 99,9')"; string[] result = Regex.Split(samplestring, ",(?=(?:[^']*'[^']*')*[^']*$)");</code>
Output:
The resulting array will contain the individual components:
<code>['ABCDEFG', '123542', 'XYZ 99,9']</code>
The above is the detailed content of How to Split Comma-Separated Strings with Embedded Commas Inside Quotes in C#?. For more information, please follow other related articles on the PHP Chinese website!