StringBuilder, append string if conditions are met
Asked Answered
O

4

9
var sb = new StringBuilder ();

if (condition1) sb.Append ("one");
if (condition2) sb.Append ("two");
if (condition3) sb.Append ("three");
if (condition4) sb.Append ("four");
if (condition5) sb.Append ("five");

return sb.ToString ();

Any idea how to improve it? How to write less code, giving same result?

Oversize answered 19/12, 2014 at 14:25 Comment(7)
This looks fine already. You could probably create an extension method, something like AppendIf(string, bool), but not sure if this is an improvement at allPrecautious
if there is no inter-dependency between the conditions, I see no way to improve things.Benetta
use a single varible and at the end just append itAlrich
You could do sb.Append(condition1 ? "One" : ""); but I'm not sure it's any better.Bullet
Just to note, there is another site dedicated to this sort of question.Sd
@BhargavModi, what if multiple conditions are true?Benetta
@Benetta than just use (confition1 || condition2)Alrich
M
15

This code is fine. Keep it like this.

  • Every attempt to use extension methods or others will only make this code less understandable and maintainable;
  • There is no repeating code;
  • As long as the conditions don't influence another, there is no way of shortening the ifs.

If you do want another option:

string s = 
   (condition1 ? "one" : null) + 
   (condition2 ? "two" : null) + 
   (condition3 ? "three" : null) + 
   (condition4 ? "four" : null) + 
   (condition5 ? "five" : null)
   ;

But let's be honest, does this make it better? No.

Misconceive answered 19/12, 2014 at 14:26 Comment(4)
you should use string.Empty instead of ""Benetta
Just wondering - if all conditions are false, will this code result in a null string rather than an empty string?Sd
@JamesThorpe: No. "". String concatenation will always result in an instantiated string.Misconceive
@PatrickHofman Thanks. Now off to investigate why that is...! ... (here's the answer)Sd
H
7

I prefer approach with simple DSLs definition, when it makes code simpler or more readable. Also "pipeline-style" expressions are awesome. In your case it can be written like this:

var str =
    new StringBuilder()
        .AppendIf(condition1, "one")
        .AppendIf(condition2, "two")
        .AppendIf(condition3, "forty two")
        .ToString();

With an extension method.

public static class StringBuilderExtensions
{
    public static StringBuilder AppendIf(
        this StringBuilder @this,
        bool condition,
        string str)
    {
        if (@this == null)
        {
            throw new ArgumentNullException("this");
        }

        if (condition)
        {
            @this.Append(str);
        }

        return @this;
    }
}

Approach is suitable here, if conditions are repeated. For example arg1 != null, arg2 != null, then AppendIfNotNull can be used.

Otherwise, think twice, because it looks quite similar to initial implementation, requires additional code, can be slower because of additional null checks and method invocations, and also you should create an AppendIf overload for every Append one.

Hootman answered 19/12, 2014 at 20:3 Comment(0)
B
3

You could do something like,

var conditions = new[]
    {
        Tuple.Create(condition1, "one"),
        Tuple.Create(condition2, "two"),
        Tuple.Create(condition3, "three"),
        Tuple.Create(condition4, "four"),
        Tuple.Create(condition5, "five"),
    }

return string.Concat(conditions.Where(t => t.Item1).Select(t => t.Item2));

Is that better? No.

Benetta answered 19/12, 2014 at 14:41 Comment(0)
R
2

Another approach is to use string interpolation like documented here https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated. So, the example can be modified like this. I think it is easier to read.

var s = $"{(condition1 ? "one": null)}{(condition2 ? "two": null)}{(condition3 ? "three": null)}{(condition4 ? "four": null)}{(condition5 ? "five": null)}";

Edit: You can make the code easier to read by making it verbatim string literal.

var s = $@"{
    (condition1 ? "one": null)
}{
    (condition2 ? "two": null)
}{
    (condition3 ? "three": null)
}{
    (condition4 ? "four": null)
}{
    (condition5 ? "five": null)}";
Restless answered 25/4, 2018 at 11:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.