如果不是String.Empty忽略空字符串 - VB.NET

pln*_*txt 5 vb.net arrays string

我有一个字符串数组,我循环遍历它们,但字符串可能是空的所以我正在尝试这个:

For Each Component As String In Components
    If Component IsNot String.Empty Then
        'Work your magic
    End If
Next
Run Code Online (Sandbox Code Playgroud)

但是如果Component是一个空字符串,逻辑仍然会触发.我也试过了

If Component <> "" Then 

End If
Run Code Online (Sandbox Code Playgroud)

结果相同.那我错过了什么?

Dan*_*May 17

  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
    
    Run Code Online (Sandbox Code Playgroud)