Corey
Corey

Reputation: 1793

Concatenating a list of strings into a single string

I am trying to concatenate a list of strings into a single string separated by a comma. Pretty straightforward using string.Join, the problem I am facing is how can I do this using a property?

public class JsonObject
{
    public string EntityID { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string Address3 { get; set; }
    public string Address3 { get; set; }

    public List<string> Category = ??
}

I am trying to get a json object and insert it into a DB. Category is an array which I can handle with List<string>. How do I concat this list of strings into a single string and then return it to string Category? I assume you would have to use a separate class to handle it but other than that I am not sure how else to go about it.

The Json Object looks like this:

"EntityID":"foo",
"Categories": [ "Category1", "Category2", "Category3"] 

It is these Categories(1,2,3) that I want to concatenate into the single string i.e.

public string Category;

Upvotes: 2

Views: 8112

Answers (4)

abatishchev
abatishchev

Reputation: 100308

If you need a separator-separated string then:

public List<string> Categories { get; set; }

public string Category
{
    get
    {
        return String.Join(",", Categories);
    }
}

If just concatenate i.e. join by empty string:

public string Category
{
    get
    {
        return String.Concat(Categories);
    }
}

Upvotes: 9

Stark
Stark

Reputation: 514

I hope i am understanding your question correctly, in that you wish to know how to do the concatenation through the class properties.

If so, then here is my suggested solution:

public class JsonObject
    {
        public string CompiledString { get; set; }

        private string _Category;
        public string Category 
        { 
            get
            { 
                return _Category; 
            }
            set
            {
                _Category = value;
                CompileString();
            }
        }

        private string _EntityID;
        public string EntityID
        {
            get
            {
                return _EntityID;
            }
            set
            {
                _EntityID = value;
                CompileString();
            }
        }
        //Rest of the properties go here

        private void CompileString()
        {
            //cycle through each of your properties and update the CompiledString variable
            CompiledString =
                _Category == null ? string.Empty : _Category + "," +
                _EntityID == null ? string.Empty : _EntityID + ",";
            //I left the last comma in there because you will be adding other props... just remember to exclude it from the last one.

            //Of course this part of the implementation is entirely up to you, your question was about how to do it through the property
        }
    }

You have a public field or property in your class which contains the concatenated value and then in your property setters you call a private method in the class which does the concatenation for you.

Upvotes: 0

Sajeetharan
Sajeetharan

Reputation: 222682

public static string Concat(this IEnumerable<string> yourString) {
    StringBuilder category= new StringBuilder();
    foreach(string s in yourString) {
        category.Append(s);
    }
    return category.ToString();
}

if you are using .net 4.0

  String.Join(String.Empty, yourString.ToArray());

Upvotes: 0

payo
payo

Reputation: 4561

First of all, I would recommend not making a list of variables that hold very similar information, rather you should group them into a single container.

public string[] Addresses { get; protected set; }

Make sure to initialize the array to the size you want in a constructor. Also, consider List if this really should be dynamic in size.

If you want a special helper to join your strings just abstract the problem away. Basically, you want to iterate over a group of strings.

protected IEnumerable<string> GetStringData()
{
  yield return EntityID;
  foreach (var address in Addresses)
    yield return address;
}

Then join the string together using string.Join

string commaDelim = string.Join(",", GetStringData())

EDIT In case you want to use the Category property as you've stated in your question:

public string Category { get { return string.Join(",", GetStringData()); } }

Upvotes: 0

Related Questions