连接字符串(不想连接空字符串)

时间:2015-03-10 14:31:54

标签: vb.net

我有5个字符串变量,我有一个时刻,我想将它们存储(连接)到一个;分隔器。但其中一些有时可能是空的(“”),我想跳过它们。到目前为止我有这个代码

  If ACL_PermissionFC.Length > 0 Then ACL_PermissionFC = ACL_PermissionFC.PadLeft(PaddingValue) & " [" & ACL_Type & "]" Else ACL_PermissionFC = ""

        If ACL_PermissionM.Length > 0 Then ACL_PermissionM = ACL_PermissionM.PadLeft(PaddingValue) & " [" & ACL_Type & "]" Else ACL_PermissionM = ""

        If ACL_PermissionRE.Length > 0 Then ACL_PermissionRE = ACL_PermissionRE.PadLeft(PaddingValue) & " [" & ACL_Type & "]" Else ACL_PermissionRE = ""

 If ACL_PermissionRE.Length > 0 Then ACL_PermissionRE = ACL_PermissionRE.PadLeft(PaddingValue) & " [" & ACL_Type & "]" Else ACL_PermissionRE = ""

        If ACL_PermissionLD.Length > 0 Then ACL_PermissionLD = ACL_PermissionLD.PadLeft(PaddingValue) & " [" & ACL_Type & "]" Else ACL_PermissionLD = ""

dim Permissions as String               
Permissions= ACL_PermissionFC & ";" & ACL_PermissionFC & ";" & ACL_PermissionRE & ";" & ACL_PermissionLD & ";" & ACL_PermissionR & ";" & ACL_PermissionW

当有空字符串时,我会得到;;Has Read & Execute security [Allow];Has List Folder Contents security [Allow];Has Read security

如果字符串为空而不连接它,我该怎么做? (我不想要双重;或者三重奏)

4 个答案:

答案 0 :(得分:4)

String.JoinWhere / SelectString.IsNullOrWhitespace一起使用:

Dim items = From item In {ACL_PermissionFC, ACL_PermissionM, ACL_PermissionRE, ACL_PermissionW, ACL_PermissionLD} 
            Where Not String.IsNullOrWhiteSpace(item)
            Select String.Format("{0}[{1}]", item.PadLeft(PaddingValue), ACL_Type)

Dim Permissions = String.Join(";", items)

答案 1 :(得分:1)

编辑:看起来懒惰不符合他的同名。当我还在检查语法时,他发布了几乎相同的答案。

您可以使用Linq消除空字符串,并使用String.Join()连接它们:

Dim Permissions As String = String.Join(";", { ACL_PermissionFC, ACL_PermissionM, ACL_PermissionRE, ACL_PermissionLD, ACL_PermissionW }.Where(Function(acl) Not String.IsNullOrWhiteSpace(acl)).ToArray())

警告:我的Visual Studio再次吓坏了,所以我没有测试上面的准确性。

答案 2 :(得分:0)

首先修剪字符串怎么样?

If ACL_PermissionFC.Trim() <> "" Then ACL_PermissionFC = ACL_PermissionFC.PadLeft(PaddingValue) & " [" & ACL_Type & "]" Else ACL_PermissionFC = ""

        If ACL_PermissionM.Trim() <> "" Then ACL_PermissionM = ACL_PermissionM.PadLeft(PaddingValue) & " [" & ACL_Type & "]" Else ACL_PermissionM = ""

        If ACL_PermissionRE.Trim() <> "" Then ACL_PermissionRE = ACL_PermissionRE.PadLeft(PaddingValue) & " [" & ACL_Type & "]" Else ACL_PermissionRE = ""

 If ACL_PermissionRE.Trim() <> "" Then ACL_PermissionRE = ACL_PermissionRE.PadLeft(PaddingValue) & " [" & ACL_Type & "]" Else ACL_PermissionRE = ""

        If ACL_PermissionLD.Trim() <> "" Then ACL_PermissionLD = ACL_PermissionLD.PadLeft(PaddingValue) & " [" & ACL_Type & "]" Else ACL_PermissionLD = ""

dim Permissions as String               
Permissions= ACL_PermissionFC & ";" & ACL_PermissionFC & ";" & ACL_PermissionRE & ";" & ACL_PermissionLD & ";" & ACL_PermissionR & ";" & ACL_PermissionW

通过这种方式,您将看到您的字符串是否完全为空。

答案 3 :(得分:0)

我不会重复这些代码。把它放在一个函数中。使用StringBuilder可能会更好。

    Dim permissions As String = ""

    permissions = ConcatenateString(permissions, ACL_PermissionFC, PaddingValue, ACL_Type)
    permissions = ConcatenateString(permissions, ACL_PermissionM, PaddingValue, ACL_Type)
    permissions = ConcatenateString(permissions, ACL_PermissionRE, PaddingValue, ACL_Type)
    permissions = ConcatenateString(permissions, ACL_PermissionRE, PaddingValue, ACL_Type)
    permissions = ConcatenateString(permissions, ACL_PermissionLD, PaddingValue, ACL_Type)

Function ConcatenateString(ByVal str As String, ByVal newValue As String, ByVal paddingValue As Integer, ByVal aclType As String) As String

    newValue = newValue.Trim() ' Needed?

    If newValue.Length > 0 Then
        newValue = newValue.PadLeft(paddingValue)

        If str.Length > 0 Then
            str &= ";"
        End If

        str &= String.Format("{0} [{1}]", newValue, aclType)
    End If

    Return str
End Function