Check for length in a string I have this logic that seems complicated checking the length and adding a colon. I'd like to see if any of you C# experts can help me simplify it. If the replace remove the full text then I am just adding the word Result. If it removes the make then I am just displaying the model.

Is there a way I can remove the if statement and combine into one? 

namespace DupName
{
    public class Program
    {
        static void Main(string[] args)
        {
            StringBuilder childName = new StringBuilder();

            List&lt;Item&gt; items = new List&lt;Item&gt;()
                .AddAlso(new Item { Id = 1, Name = "Audi", ChildName = "Audi A3 Premium Plus" })
                .AddAlso(new Item { Id = 2, Name = "Audi", ChildName = "Audi" })
                .AddAlso(new Item { Id = 1, Name = "Audi", ChildName = "Audi" })
                .AddAlso(new Item { Id = 2, Name = "Audi", ChildName = "S5 Premium Plus 3.0 TFSI quattro" })
                .AddAlso(new Item { Id = 2, Name = "Audi", ChildName = "S3 Premium Plus 3.0 TFSI quattro" });

            foreach (var item in items)
            {

                string temp = item.ChildName.Replace(item.Name, "").Trim();

                if (temp.Length == 0)
                {
                    Console.WriteLine(string.Format("{0}","Result"));

                }else
                {

                    Console.WriteLine(string.Format("{0}: {1} - {2}", "Result", temp, item.Name));
                }
            }
        }
    }

    public static class Extensions
    {
        public static List&lt;T&gt; AddAlso&lt;T&gt;(this List&lt;T&gt; list, T item)
        {
            list.Add(item);
            return list;
        }
    }

    public class Item
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string ChildName { get; set; }
    }


Output



Result: A3 Premium Plus - Audi
Result
Result
Result: S5 Premium Plus 3.0 TFSI quattro - Audi
Result: S3 Premium Plus 3.0 TFSI quattro - Audi
If I understand correctly, your frustration is with the necessary computation for knowing whether to print Result or Result: temp - childname. That's because you're doing more steps than necessary.

Instead of:


Calculating the (ChildName - Name) string
Trimming any whitespaces in the resulting string
And then checking if this string is empty


You could just check if both the Name and ChildName are equal. If they are, your variable temp isn't used. Also, since result is written independently of such calculation, you could always print it.

So, I would suggest exchanging

foreach (var item in items) {
    string temp = item.ChildName.Replace(item.Name, "").Trim();
    if (temp.Length == 0) {
        Console.WriteLine(string.Format("{0}","Result"));
    } else {
        Console.WriteLine(string.Format("{0}: {1} - {2}", "Result", temp, item.Name));
    }
}


for

foreach (var item in items) {
    Console.Write("Result");

    if (item.Name != item.ChildName) {
        string childNameWithoutName = item.ChildName.Replace(item.Name, "").Trim();
        Console.Write(string.Format(": {1} - {2}", childNameWithoutName, item.Name));
    }

    Console.WriteLine();
}