Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using .Split to remove empty entries

Tags:

split

vb.net

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?

like image 514
Cyclone Avatar asked Sep 10 '09 23:09

Cyclone


People also ask

How do I remove blank strings from a list?

Method #1: Using remove() This particular method is quite naive and not recommended use, but is indeed a method to perform this task. remove() generally removes the first occurrence of an empty string and we keep iterating this process until no empty string is found in list.

Can you split an empty string?

The split() method does not change the value of the original string. If the delimiter is an empty string, the split() method will return an array of elements, one element for each character of string. If you specify an empty string for string, the split() method will return an empty string and not an array of strings.

How do you remove spaces in Split?

To split the sentences by comma, use split(). For removing surrounding spaces, use trim().

How do you split a string and delete spaces?

To split a string and trim the surrounding spaces: Call the split() method on the string. Call the map() method to iterate over the array. On each iteration, call the trim() method on the string to remove the surrounding spaces.


2 Answers

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"})

like image 186
Ken Browning Avatar answered Sep 21 '22 06:09

Ken Browning


Try:

TextBox1.Text.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Length 
like image 40
Jay Riggs Avatar answered Sep 20 '22 06:09

Jay Riggs