gpt4 book ai didi

c# - 如何将字符串数组转换为数组

转载 作者:太空宇宙 更新时间:2023-11-03 18:53:22 24 4
gpt4 key购买 nike

我有字符串:["gm-63.pdf","GM413.pdf","mh524.pdf"]如何使用 C# 将上述字符串转换为字符串数组?

最佳答案

可能有很多方法可以做到这一点。

选项 1

var input = @"[""gm-63.pdf"",""GM413.pdf"",""mh524.pdf""]";
var result = input.Trim('[', ']')
.Split(",")
.Select(x => x.Trim('"'))
.ToArray();

.Net 4.5 demo here

更新

Windows 禁止的文件名字符:

< (less than)
> (greater than)
: (colon - sometimes works, but is actually NTFS Alternate Data Streams)
" (double quote)
/ (forward slash)
\ (backslash)
| (vertical bar or pipe)
? (question mark)
* (asterisk)

如果这些是有效的文件名,双引号应该没有问题,但逗号可能是个问题。

所以谨慎使用

选项 2

您也可以使用 Json.Net 包。 Relevant documentation here

return JsonConvert.DeserializeObject<string[]>(Input);

基准

只是因为我很无聊,所以打开了我的基准测试器

所有测试都被垃圾收集,结果按操作缩放

----------------------------------------------------------------------------
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
----------------------------------------------------------------------------

测试 1

--- Standard input ---------------------------------------------------------
| Value | Average | Fastest | Cycles | Garbage | Test | Gain |
--- Scale 10 ------------------------------------------------ Time 0.257 ---
| Split | 519.472 ns | 390.200 ns | 2.063 K | 811.776 B | Base | 0.00 % |
| Json | 914.331 ns | 810.500 ns | 3.419 K | 819.200 B | N/A | -76.01 % |
--- Scale 100 ----------------------------------------------- Time 0.061 ---
| Split | 216.935 ns | 141.090 ns | 769.172 | 327.680 B | Base | 0.00 % |
| Json | 326.246 ns | 237.150 ns | 1.147 K | 163.840 B | N/A | -50.39 % |
--- Scale 1,000 --------------------------------------------- Time 0.101 ---
| Split | 126.233 ns | 112.272 ns | 433.772 | 256.176 B | Base | 0.00 % |
| Json | 186.400 ns | 166.907 ns | 638.003 | 81.968 B | N/A | -47.66 % |
--- Scale 10,000 -------------------------------------------- Time 0.456 ---
| Split | 114.009 ns | 106.419 ns | 388.291 | 263.059 B | Base | 0.00 % |
| Json | 168.608 ns | 154.450 ns | 574.312 | 82.574 B | N/A | -47.89 % |
--- Scale 100,000 ------------------------------------------- Time 5.545 ---
| Json | 206.117 ns | 199.416 ns | 701.200 | 75.731 B | N/A | 24.02 % |
| Split | 271.281 ns | 257.446 ns | 810.403 | 144.212 B | Base | 0.00 % |
----------------------------------------------------------------------------

总结

如您所见,JsonConvert 一开始速度较慢,但​​它的扩展性很好,它还可以避免转义问题(如前所述),您或许应该使用它。它的代码也更少。

祝你好运

关于c# - 如何将字符串数组转换为数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51568033/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com