如果不是 String.Empty 忽略空字符串 - VB.NET
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3461385/
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
If Not String.Empty ignoring empty string - VB.NET
提问by plntxt
I have a array of strings and I am looping through them, but the string might be empty so I am trying this:
我有一个字符串数组,我正在遍历它们,但字符串可能为空,所以我正在尝试:
For Each Component As String In Components
If Component IsNot String.Empty Then
'Work your magic
End If
Next
But if Component is an empty string the logic still fires. I've also tried
但是如果 Component 是一个空字符串,逻辑仍然会触发。我也试过
If Component <> "" Then
End If
With the same results. So what am I missing?
结果相同。那么我错过了什么?
回答by Daniel May
- Make sure that your List is of type
string
Use the String.IsNullOrEmpty method.
Sub Main Dim foo As String foo = "Non-Empty string" If Not String.IsNullOrEmpty(foo) Then Console.WriteLine("Foo is not empty.") End If End Sub
- 确保您的列表类型
string
使用 String.IsNullOrEmpty 方法。
Sub Main Dim foo As String foo = "Non-Empty string" If Not String.IsNullOrEmpty(foo) Then Console.WriteLine("Foo is not empty.") End If End Sub
回答by Wade73
One thing that has gotten me before is spaces. You can't see it when you view the variable in the watch window, but it makes the string not empty or null.
以前让我感兴趣的一件事是空格。在监视窗口中查看变量时看不到它,但它使字符串不为空或为空。
回答by Matt
Do your string have default values and are they actually ""? What if you used:
您的字符串是否具有默认值,它们实际上是“”吗?如果你使用了怎么办:
If Not Component Is Nothing Then
End If