2

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)

2
  • answers are good enough for your answer, but i would totally follow a different design such as using nodes and graphs. Commented Aug 19, 2013 at 20:04
  • 1
    Just check for IsNullOrEmpty prior to concatenating each one. It won't all be on one line, but that doesn't matter. Commented Aug 19, 2013 at 20:05

2 Answers 2

6

In C#:

 string.Join(" OR ", new[] { stringA, stringB }
      .Where(s => !string.IsNullOrEmpty(s)));

Note that this will work with larger arrays too. Add .ToArray() in case you're on .NET <4.0

Sign up to request clarification or add additional context in comments.

2 Comments

Would a trasnlation to vb be String.Join(" OR ", New (){stringA, stringB}.Where(Function(s) Not String.IsNullOrEmpty(s))).ToArray() ?? I am using .NET 2
@cMinor Move the .ToArray inside the parens: String.Join(" OR ", New (String.Empty){stringA, stringB}.Where(Function(s) Not String.IsNullOrEmpty(s)).ToArray()) (you want to affect the String[] array parameter to String.Join
0

I just want to throw something out in case you are planning to execute this against a database or something along those lines. Simply putting an "OR" between the clauses is not going to return the results you expect. You need to put parenthesis around the conditions first to preserve the logic as you have it.

Most like you mean something like:

(db = 45 AND frec = 500) OR (db = 25 AND frec = 1) OR (....

Just be careful, or you may be surprised by what you get back. Good luck!

5 Comments

AND has higher precedence than OR, so your example is exactly equal to db = 45 AND frec = 500 OR db = 25 AND frec = 1 OR ....
@jacob-krall That is true for evaluating logic in C# for example, but that is false for SQL syntax. Try running a query against SQL Server and check the results, or search the web (the first article I found on it was here.
the article you linked very clearly shows that OR has lower precedence than AND. That is why they had to put parentheses around the OR clause.
Here is the TechNet documentation: technet.microsoft.com/en-us/library/ms190276.aspx
@jacob-krall I stand corrected!! I have seen issues in the past with this and have always included parenthesis--I must have not been paying attention to the AND/OR order of precendence. Thanks for pointing this out!

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.