40

I am creating simple webform in C#. Here I am getting full address by concatenating which works well. But let's say if I don't have address2, city, etc, then I want to skip appending commas at end of each string (e.g. if address1 is null or empty).

string address1 = "Address1";
string address2 = "Address2";
string city = "City";
string country = "Country";
string postalCode = "00000";

string fullAddress = ? --> address1 + ","+ address2 +","+ city  and so on
2
  • Possible duplicate of String.Join method that ignores empty strings? Commented Oct 9, 2018 at 13:11
  • 1
    @Hermann.Gruber Whilst incredibly similar and very much overlapping I think this question (and the subsequent answers) show that this can be done on a selection of variables and not only on an array (albeit several of the answers create an array as part of the process) making this question worthy of keeping in its own right. Commented Nov 24, 2020 at 17:30

6 Answers 6

99

If you want to remove the empty or null string you have to filter the array used in the join method:

var array = new[] { address1, address2, city, country, postalCode };
string fullAddress = string.Join(",", array.Where(s => !string.IsNullOrEmpty(s)));

If we make city="" the we have Address1,Address2,Country,00000

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

Comments

24

you could use string.join along with a filter to remove the duplicated commas when one or more of the values are null or empty.

Console.WriteLine(string.Join(",", new string[] { address1 , address2 , city , country , postalCode }.Where(c => !string.IsNullOrEmpty(c))));

1 Comment

Upvoted for being 5m earlier than the accepted answer and doing it all on one line like a ninja.
4

Try this:

string address1 = "Address1";
string address2 = "Address2";
string city = "";
string country = "Country";
string postalCode = "00000";

Func<string, string> f = s => string.IsNullOrEmpty(s) ? string.Empty : string.Format("{0},", s);
string fullAddress = string.Format("{0}{1}{2}{3}{4}", f(address1), f(address2), f(city), f(country), f(postalCode)).Trim(',');

1 Comment

This is the right answer. It accounts for the variables being null.
2

You can use string.Join to do your task. You can run in dotnetfiddle. Please check below code:

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    public static void Main()
    {
        string address1 = "Address1";
        string address2 = "Address2";
        string city = "City";
        string country = "Country";
        string postalCode = "00000";

        List<string> strArray = new List<string> { address1, address2, city, country, postalCode };

        string fullAddress = string.Join(",", strArray.Where(m=> !string.IsNullOrEmpty(m)).ToList());

        Console.WriteLine(fullAddress);
    }
}

1 Comment

I've updated my answer, it will ignore empty value.
1

String.Join is what you need.

string address1 = "Address1";
string address2 = "Address2";
string city = "City";
string country = "Country";
string postalCode = "00000";

string[] stuff = new string [] { address1, address2, city, country, postalCode };

string fulladdress = string.Join(",", stuff).Replace(",,",",");

3 Comments

Your answer does not account for null variables.
That does not remove the duplicated commas when one of the values is empty
That's because a string is a nullable type. string.Join replaces null parameter values with "string.Empty" with the assumption that other code is determining null handling. If the array itself is null, then an exception is thrown. I'd suggest as a matter of separation of concerns, following with string.Replace() replacing ",," with "," would better address null values. I Updated the answer with this.
0

You can put all your elements in an array and join the array with the ",". In this case, the commas will be well placed, betweed your different address part.

Comments

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.