vb.net 使用 .Split 删除空条目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1408321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:25:21  来源:igfitidea点击:

Using .Split to remove empty entries

vb.netsplit

提问by Cyclone

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?

我究竟做错了什么?

回答by Ken Browning

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

好吧,该Split函数的第一个参数需要是一个字符串或字符数组。尝试:

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

当您之前没有指定第二个参数时,您可能没有注意到这一点。这是因为该Split方法有一个重载,它接受一个 ParamArray。这意味着调用Split("string 1", "string 2", "etc")自动神奇地转换为调用Split(New String() {"string 1", "string 2", "etc"})

回答by Jay Riggs

Try:

尝试:

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

回答by o.k.w

This is what I did:

这就是我所做的:

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

Result: Length = 5

结果:长度 = 5

回答by Mr. Mann

// 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);