如何将逗号分隔的字符串转换为整数列表?

use*_*496 -1 c# string

如何将字符串var numbers = "2016, 2017, 2018";转换为List<int>

我试过这个:

List<int> years = Int32.Parse(yearsString.Split(',')).ToList();
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误消息:

无法从string []转换为字符串.

AAA*_*ddd 7

像这样的东西

var results = yearsString.Split(',').Select(x => int.Parse(x.Trim()));
Run Code Online (Sandbox Code Playgroud)

注意:在这种方法中没有容错,如果它们无法转换为int此类,则会出现异常情况

只是为了好玩,这是一种使用指针unsafe和方法的方法fixed

fixed (char* pInput = Input)
{
   var len = pInput + Input.Length;
   var current = 0;
   var results = new List<int>();

   for (var p = pInput; p < len; p++)
   {
      if (*p >= 48 & *p <= 58)
         current = current * 10 + *p - 48;          
      else if (*p == ',')
      {
         results.Add(current);
         current = 0;
      }
   }

   results.Add(current);
   return results;
}
Run Code Online (Sandbox Code Playgroud)

基准

只因为我很无聊

----------------------------------------------------------------------------
Mode             : Release (64Bit)
Test Framework   : .NET Framework 4.7.1 (CLR 4.0.30319.42000)
----------------------------------------------------------------------------
Operating System : Microsoft Windows 10 Pro
Version          : 10.0.17134
----------------------------------------------------------------------------
CPU Name         : Intel(R) Core(TM) i7-2600 CPU @ 3.40GHz
Description      : Intel64 Family 6 Model 42 Stepping 7
Cores (Threads)  : 4 (8)      : Architecture  : x64
Clock Speed      : 3401 MHz   : Bus Speed     : 100 MHz
L2Cache          : 1 MB       : L3Cache       : 8 MB
----------------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

测试1

--- Standard input --------------------------------------------------------
| Value  |    Average |    Fastest |    Cycles | Garbage | Test |    Gain |
--- Scale 100 ---------------------------------------------- Time 0.003 ---
| Unsafe |   2.304 µs |   2.101 µs |   9.298 K | 0.000 B | Pass | 90.28 % |
| Linq   |  23.711 µs |  22.214 µs |  82.700 K | 0.000 B | Base |  0.00 % |
--- Scale 1,000 -------------------------------------------- Time 0.046 ---
| Unsafe |  21.697 µs |  21.013 µs |  75.490 K | 0.000 B | Pass | 90.41 % |
| Linq   | 226.218 µs | 205.332 µs | 768.447 K | 0.000 B | Base |  0.00 % |
--- Scale 10,000 ------------------------------------------- Time 0.250 ---
| Unsafe | 214.526 µs | 200.829 µs | 733.557 K | 0.000 B | Pass | 89.93 % |
| Linq   |   2.130 ms |   1.996 ms |   7.257 M | 0.000 B | Base |  0.00 % |
--- Scale 100,000 ------------------------------------------ Time 2.906 ---
| Unsafe |   2.303 ms |   2.063 ms |   7.680 M | 0.000 B | Pass | 90.99 % |
| Linq   |  25.571 ms |  22.624 ms |  84.808 M | 0.000 B | Base |  0.00 % |
--- Scale 1,000,000 --------------------------------------- Time 36.594 ---
| Unsafe |  23.061 ms |  21.910 ms |  78.356 M | 0.000 B | Pass | 93.07 % |
| Linq   | 332.639 ms | 274.595 ms |   1.055 B | 0.000 B | Base |  0.00 % |
---------------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)