Since I like to Split()
string
s, 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...
“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.
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.
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.
Especially for multiple elements, the following shortcut is nice:
";".ToCharArray()
You can use this with multiple chars:
";,\t".ToCharArray()
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(',')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With