P. Pogba
P. Pogba

Reputation: 175

How to convert string array to array

I have string: ["gm-63.pdf","GM413.pdf","mh524.pdf"] How can I convert above string to string array with c#?

Upvotes: 3

Views: 8793

Answers (2)

TheGeneral
TheGeneral

Reputation: 81573

There are probably many ways to do this.

Options 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

Update

Windows forbidden filename characters:

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

If these are valid file names you shouldn't have a problem with Double quotes, however commas may be a problem.

So use with caution

Option 2

You could also use, with the Json.Net package. Relevant documentation here

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

Benchmarks

And just because i'm bored, and had my benchmarker open

All Tests are garbage collected, and results are scaled per operation

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

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

Summary

As you can see, the JsonConvert is slower at first, but it scales well, it would also save you escaping issues (as been discussed) and you should probably use it. Also its less code.

Good luck

Upvotes: 8

pid
pid

Reputation: 11607

Assuming this is actual JSON, I'd recommend to use a JSON parser.

Other solutions with Split/Select etc. are doomed to fail, because they do not consider escaped characters and other specifics of JSON, such as inline double-quotes " and other escaped UTF-8 surrogates.

The full solution is here.

On the other hand, if it's just a string that happens to look like JSON, you may as well use any other method described in other answers.

Upvotes: 1

Related Questions