Reputation: 35
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)
Upvotes: 1
Views: 535
Reputation: 101042
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)
Upvotes: 4
Reputation: 12748
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
Upvotes: 0
Reputation: 1350
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.
Upvotes: 0
Reputation: 76
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.
Upvotes: 1