Reputation: 18295
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?
Upvotes: 8
Views: 26056
Reputation: 11
// char array is used instead of normal char because ".Split()"
// accepts a char array
char[] c = new char[1];
//space character in array
c[0] = ' ';
// a new string array is created which will hold whole one line
string[] Warray = Line.Split(c, StringSplitOptions.RemoveEmptyEntries);
Upvotes: 1
Reputation: 29091
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"})
Upvotes: 21
Reputation: 25810
This is what I did:
TextBox1.Text = "1 2 3 5 6"
TextBox1.Text.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Length
Result: Length = 5
Upvotes: 3
Reputation: 53593
Try:
TextBox1.Text.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Length
Upvotes: 7