Convert Comma-Separated String to Integer Array in C# (One-Liner)
You can convert a string of comma-separated numbers into an array of integers using one line of code in C# by using the `Split` and `Select` methods from the LINQ library. Here's an example:
`csharp string input = "20106,20303"; int[] numbers = input.Split(',').Select(int.Parse).ToArray(); `
Explanation:
- The `Split(',')` method splits the input string at each comma, resulting in an array of substrings.
- The `Select(int.Parse)` method applies the `int.Parse` function to each substring, converting it into an integer.
- The `ToArray()` method converts the enumerable sequence of integers into an array.
Now, the `numbers` array will contain the integers [20106, 20303].
原文地址: https://www.cveoy.top/t/topic/pEnj 著作权归作者所有。请勿转载和采集!