37

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
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
sgl
  • 563
  • 1
  • 6
  • 16
  • Possible duplicate of [String.Join method that ignores empty strings?](https://stackoverflow.com/questions/16325995/string-join-method-that-ignores-empty-strings) – Hermann.Gruber Oct 09 '18 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. – Caltor Nov 24 '20 at 17:30

6 Answers6

92

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

Selim Yildiz
  • 5,254
  • 6
  • 18
  • 28
Alessandro D'Andria
  • 8,663
  • 2
  • 36
  • 32
23

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))));
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
  • 3
    Upvoted for being 5m earlier than the accepted answer and doing it all on one line like a ninja. – Caltor Nov 24 '20 at 17:26
3

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(',');
mm8
  • 163,881
  • 10
  • 57
  • 88
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);
    }
}
csharpbd
  • 3,786
  • 4
  • 23
  • 32
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(",,",",");
CDove
  • 1,940
  • 10
  • 19
  • Your answer does not account for null variables. – Rafael Apr 20 '17 at 15:42
  • That does not remove the duplicated commas when one of the values is empty – Rufus L Apr 20 '17 at 15:44
  • 1
    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. – CDove Apr 20 '17 at 15:46
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.

Titouan56
  • 6,932
  • 11
  • 37
  • 61