More newb C# questions

Tweak155

Lifer
Sep 23, 2003
11,448
262
126
I'm headed back to my C# program from a while back at work. I'll probably have newb questions for a while... here's the first!

See the following:

Code:
string hello = "HelloworldHellofriendsHelloPeople";
var a = hello.Split(new string[] { "Hello"}, StringSplitOptions.RemoveEmptyEntries);

Why do I need to do new string[] {} around "Hello" to get it to return a string array to var a?
 
Last edited:

Tweak155

Lifer
Sep 23, 2003
11,448
262
126
Because the split method requires an array of strings that act as delimiters to determine where to split the string you calling split on.

http://msdn.microsoft.com/en-us/library/tabh47cf.aspx

I thought it only required a char and it was a fancy way to "trick" it, lol.

For example, I can do:

someStr.Split('c')

That's generally how I used it before in other languages, and it also works in C#. Why does it need an array of strings but only a single character? I guess it doesn't make sense to me.
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
I thought it only required a char and it was a fancy way to "trick" it, lol.

For example, I can do:

someStr.Split('c')

That's generally how I used it before in other languages, and it also works in C#. Why does it need an array of strings but only a single character? I guess it doesn't make sense to me.

Well there are several overloads to the Split method. Some of them take character arrays and some of them take string arrays, depending on your scenario you could use one or the other.

It's important to recognize the differences between

character: ','
character array: ['.', ';', ':']
string: "this is a string"
string array: ["string 1", "string 2", "string 3"]

Strings are the one thing that most languages handle slightly differently. MSDN has a good guide on strings in C# and how they work.

http://msdn.microsoft.com/en-us/library/vstudio/ms228362.aspx
 

Tweak155

Lifer
Sep 23, 2003
11,448
262
126
Well there are several overloads to the Split method. Some of them take character arrays and some of them take string arrays, depending on your scenario you could use one or the other.

It's important to recognize the differences between

character: ','
character array: ['.', ';', ':']
string: "this is a string"
string array: ["string 1", "string 2", "string 3"]

Strings are the one thing that most languages handle slightly differently. MSDN has a good guide on strings in C# and how they work.

http://msdn.microsoft.com/en-us/library/vstudio/ms228362.aspx

Ok, so it is moreso because it is specifically a string delimiter.
 

clamum

Lifer
Feb 13, 2003
26,255
403
126
I've kinda wondered this before too, I mean, why the Split() method can take a single character: 'c' and not a single string: "str". Instead, if you want to use a string, you gotta make a new array. Just seems weird to me.
 

Tweak155

Lifer
Sep 23, 2003
11,448
262
126
I've kinda wondered this before too, I mean, why the Split() method can take a single character: 'c' and not a single string: "str". Instead, if you want to use a string, you gotta make a new array. Just seems weird to me.

At least I'm not alone :biggrin:
 

Tencntraze

Senior member
Aug 7, 2006
570
0
0
I forget about the string array part all the time, and I've been writing C# since 2005. Would love to have a single string overload. Could always add an extension method.
 

Tweak155

Lifer
Sep 23, 2003
11,448
262
126
I forget about the string array part all the time, and I've been writing C# since 2005. Would love to have a single string overload. Could always add an extension method.

I was hoping it would be some concept to C# that I needed to understand, that I could then apply other places.

Doesn't seem like it, though.
 

PhatoseAlpha

Platinum Member
Apr 10, 2005
2,131
21
81
I figure it doesn't take a single string as that might confuse people. If I were to pass in "ABC", would it be splitting on "ABC", or "A", "B", and "C"? Better to just limit it so that anybody using it for strings knows it's always the latter.
 

PrincessFrosty

Platinum Member
Feb 13, 2008
2,301
68
91
www.frostyhacks.blogspot.com
I'm headed back to my C# program from a while back at work. I'll probably have newb questions for a while... here's the first!

See the following:

Code:
string hello = "HelloworldHellofriendsHelloPeople";
var a = hello.Split(new string[] { "Hello"}, StringSplitOptions.RemoveEmptyEntries);

Why do I need to do new string[] {} around "Hello" to get it to return a string array to var a?

Remember that 'string[]' is an array of potentially many strings and the '{ "hello" }' is declaring the values. It's an array because you can pass the split method an arbitrary number of strings to split on.

Splitting on a single string is an exception to a more general function.
 

milee

Member
Jun 7, 2013
52
0
0
@Tweak Yes it is a concept that you need to understand and can apply in other places.

@PhatoseAlpha It has nothing to do with confusing people about string/char.

The Split method has various overloads, some that take a string array (string[]) as the first parameter and some that take a char array (char[]) as the first parameter.

The reason you can do:

s.Split('c')

but not

s.Split("str")

is that, of all of the method's overloads, there's one that takes a single parameter, a char[] parameter, and that parameter is declared as params. params lets you pass an array as individual parameters as in:

s.Split(new char[] { 'a', 'b', 'c', 'd' })

can be written

s.Split('a', 'b', 'c', 'd')

The reason you can't use the same syntax with those other overloads is that you can only mark an array parameter as params if it is the last parameter in the method signature, otherwise it would be impossible to map the overload to the correct implementation. So you can only use it with chars but not with strings because there's no overload of Split that takes a single string array or a string array as the last parameter (and marked as params).
 
Last edited:

Tweak155

Lifer
Sep 23, 2003
11,448
262
126
So in other words, 'c' is a character array of size 1, but "str" is not a string array of size 1?
 

milee

Member
Jun 7, 2013
52
0
0
No, 'c' is just a character and "str" is just a string. None of them are arrays (given your context).

The difference is in the method signature. Take a look on MSDN at the Split method overloads. You'll see there are a few, but there's just one that takes a single parameter and that parameter is a char array (char[]) and also marked as params.

That parameter, being marked as params lets you use a parameter sequence of the type that the array holds. So instead of forcing you to call the method like:

s.Split(new char[] {'a', 'b'})

you can call it like:

s.Split('a', 'b')

and the compiler post processes your 'a' and 'b' into an array.

So these two ways of calling the Split method with 'a' and 'b' are equivalent.

The same goes for:

s.Split('c')

and

s.Split(new char[] { 'c' })

These two are also equivalent but the first one is easier to write and read, and also the one that creates the confusion.

----------------

Yes, 'c' passed that way gets packed into a char array of length 1. Also yes, you could pass "str" to the method call and that would also get packed into a string array of length 1. The reason you can't pass "str" is that there is NO Split method signature that takes just a params string[] but there IS one that takes just a params char[].
 
Last edited: