add string between strings if they are not null or empty
Supossing I have 4 strings I want to add an OR operator between them:
Dim s1="db = 45 AND frec = 500 "
Dim s2="db = 25 AND frec = 1 "
Dim s3="db = 5 AND frec = 2 "
Dim s4="db = 15 AND frec = 4 "
so
Dim result = "db = 45 AND frec = 500 OR db = 25 AND frec = 1 OR db = 5 AND
frec = 2 OR db = 15 AND frec = 4"
Thw woul be easy as concatenating strings
Dim result= s1 & " OR " & s2 & " OR " & s3 & " OR " & s4
However in general, any of strings could be empty or be null so if I
concatenate empty strings I would get
For instance s2 = ""
Dim result = "db = 45 AND frec = 500 OR OR db = 5 AND frec = 2 OR db = 15
AND frec = 4"
which is incorrect, I was thinking to replace strings like "OR OR"
Dim result = result.Replace("OR OR", "")
Is there a better approach? a quick solution would be to hard code all
cases but guess that is not so good
(I cannot change the design of this, as the strings are used on several
other things)
No comments:
Post a Comment