代码之家  ›  专栏  ›  技术社区  ›  personaelit

if not string.empty忽略空字符串-vb.net

  •  4
  • personaelit  · 技术社区  · 14 年前

    我有一个字符串数组,正在循环使用它们,但字符串可能是空的,因此我正在尝试:

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

    但是,如果组件是空字符串,则逻辑仍会激发。我也试过了

    If Component <> "" Then 
    
    End If
    

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

    3 回复  |  直到 14 年前
        1
  •  16
  •   Daniel May    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
      
        2
  •  1
  •   Wade73    14 年前

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

        3
  •  0
  •   Matt    14 年前

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

    If Not Component Is Nothing Then
    
    End If