tags:

views:

805

answers:

3

I am trying to split at every space " ", but it will not let me remove empty entries and then find the length, but it is treated as a syntax error.

My code:

TextBox1.Text.Split(" ", StringSplitOptions.RemoveEmptyEntries).Length

What am I doing wrong?

+3  A: 

Well, the first parameter to the Split function needs to be an array of strings or characters. Try:

TextBox1.Text.Split(New String() {" "}, StringSplitOptions.RemoveEmptyEntries).Length

You might not have noticed this before when you didn't specify the 2nd parameter. This is because the Split method has an overload which takes in a ParamArray. This means that calls to Split("string 1", "string 2", "etc") auto-magically get converted into a call to Split(New String() {"string 1", "string 2", "etc"})

Ken Browning
That did it!
Cyclone
+1  A: 

Try:

TextBox1.Text.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Length
Jay Riggs
A: 

This is what I did:

TextBox1.Text = "1 2 3  5 6"
TextBox1.Text.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Length

Result: Length = 5

o.k.w