带有空变量的字符串

时间:2015-06-17 08:43:17

标签: vb.net string

嘿我正在制作一个字符串,但有些变量将是空的,我希望这些可以跳过,但不知道如何解决这个问题。这是下面字符串的代码。

Dim TenantList As String = Ten11 & "," & Ten10 & "," & Ten9 & "," & Ten8 & "," & Ten7 & "," & Ten6 & "," & Ten5 & "," & Ten4 & "," & Ten3 & "," & Ten2 & "," & Ten1

1 个答案:

答案 0 :(得分:3)

您可以将它们放入集合中并使用String.Join

Dim allStrings As String() = {Ten11, Ten12, Ten13, Ten14, ...}
Dim notEmpty = From str In allStrings Where Not String.IsNullOrEmpty(str)
Dim TenantList As String = String.Join(",", notEmpty)

我正在使用LINQ过滤掉空字符串,因此您需要Imports System.Linq