Shortcut for creating character array
Asked Answered
S

2

23

Since I like to Split() strings, I usually use

new char[] { ';' }

or something like that for a parameter for Split().

Is there any shortcut for creating a character array with one element at compile time? Not that I mind typing, but...

Sonority answered 22/7, 2012 at 21:59 Comment(0)
A
41

Especially for multiple elements, the following shortcut is nice:

";".ToCharArray()

You can use this with multiple chars:

";,\t".ToCharArray()
Antimasque answered 22/7, 2012 at 22:2 Comment(1)
Great shortcut. I will never forget this one :)Tensor
H
22

In C# 3, you can use an implicitly-typed array:

new[] { ';' }

If you're not passing a StringSplitOptions, you can simply take advantage of the params parameter:

.Split(',')
Horst answered 22/7, 2012 at 22:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.