1

I have 5 string variables, and i have a moment which I want to store (concatenate) them into one with ; separator. but some of them sometimes might be empty ( "" ) and i want to skip them. so far I have this code

  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

And when there are empty strings i get this ;;Has Read & Execute security [Allow];Has List Folder Contents security [Allow];Has Read security

how can i make it when a string is empty not to concatenate it? ( i dont want double ; or tripple)

2
  • Check out String.Join and String.Split. Commented Mar 10, 2015 at 14:38
  • A class or List for 5 things so closely related would seem to be more appropriate than 5 independent vars (or Dictionary or even an array) Commented Mar 10, 2015 at 14:39

4 Answers 4

4

Use String.Join together with Where/Select and String.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)
Sign up to request clarification or add additional context in comments.

1 Comment

Although the one-liner works i prefer a more readable version ;)
1

Edit: It looks like sloth isn't living up to his namesake. He posted pretty much the same answer while I was still checking syntax.

You can use Linq to eliminate the empty strings, and String.Join() to concatenate them:

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

Caveat: My Visual Studio is freaking out again, so I haven't tested the above for accuracy.

1 Comment

You are too late. Also, ToArray is redundant if you use at least NET 4.
0

How about trimming the string first?

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

This way you will see if your string is completely empty or not.

Comments

0

I wouldn't repeat the code. Put it in a function. This could be better with a 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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.