2010-08-11 19 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