2010-08-11 84 views
4

我有一个字符串的数组,我通过他们循环,但字符串可能是空的,所以我想这一点:如果不忽略的String.Empty空字符串 - VB.NET

For Each Component As String In Components 
    If Component IsNot String.Empty Then 
     'Work your magic 
    End If 
Next 

但是,如果组件是逻辑仍然触发的空字符串。我也试过

If Component <> "" Then 

End If 

具有相同的结果。那么我错过了什么?

+0

只需添加完整性:假设Component为空字符串必须为假。它可以是非空字符串,也可以是Nothing。 – jeroenh 2010-08-11 19:33:45

回答

14
  1. 请确保您的列表是类型string
  2. 使用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 
    
+0

谢谢大家。 – plntxt 2010-08-11 18:15:38

0

做你的字符串有默认值,是他们实际上是 “”?如果你使用:

If Not Component Is Nothing Then 

End If 
1

有一件事是我之前得到的是空间。当您在监视窗口中查看变量时看不到它,但它使字符串不为空或为空。

+0

那不是,而是提示的+1。 – plntxt 2010-08-11 18:17:16