Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shortcut for creating character array

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

like image 619
Daniel Mošmondor Avatar asked Jul 22 '12 21:07

Daniel Mošmondor


People also ask

How do you create an array of characters?

“array_name = new char[array_length]” is the syntax to be followed. Anyways we can do both declaration and initialization in a single by using “char array_name[] = new char[array_length]” syntax. The length of the array should be declared at the time of initialization in a char array.

How do you declare a character array in C?

char arr[] = {'c','o','d','e','\0'}; In the above declaration/initialization, we have initialized array with a series of character followed by a '\0' (null) byte. The null byte is required as a terminating byte when string is read as a whole.

What is the correct way to initialize character array?

An array of character type may be initialized by a character string literal or UTF−8 string literal, optionally enclosed in braces. Successive bytes of the string literal (including the terminating null character if there is room or if the array is of unknown size) initialize the elements of the array.


2 Answers

Especially for multiple elements, the following shortcut is nice:

";".ToCharArray()

You can use this with multiple chars:

";,\t".ToCharArray()
like image 187
usr Avatar answered Oct 05 '22 10:10

usr


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(',')
like image 25
SLaks Avatar answered Oct 05 '22 12:10

SLaks