Click here to Skip to main content
15,861,168 members
Articles / Programming Languages / C# 4.0
Tip/Trick

Few extension methods of String concatenation in C#

Rate me:
Please Sign up or sign in to vote.
4.80/5 (4 votes)
19 Jun 2011CPOL2 min read 33.5K   9   9
Few extension methods of String concatenation in C#
String concatenation

About string concatenation, there are already many articles that have been posted in the web. There are many arguments about string concatenation, for example some people say we should not use +, some says use string.join or StringBuilder. I think all are right because usage of different techniques depend on the situation and expected performance really. I reflect the string.concat method using .NET reflector then found
C#
return (arg0.ToString() + arg1.ToString());
code in where they use + so then what's wrong with the + ? Of course, there are performance issues with +.

So I have created few extensions of string to do the concatenation operation which I include below:

C#
public static class StringExtensions
{

        public static string JoinWithSeparator(this string baseItem, string firstItem, string separator = default(string))
        {
            return string.Join(separator: separator, value: new[] { baseItem, firstItem });
        }

        public static string JoinWithSeparator(this string baseItem, string firstItem, string secondItem, string separator = default(string))
        {
            return string.Join(separator: separator, value: new[] { baseItem, firstItem, secondItem });
        }

        public static string JoinWithSeparator(this string baseItem, string[] items, string separator = default(string))
        {
            return string.Join(separator, new[] { baseItem, string.Join(separator: separator, value: items) });
        }

        public static string JoinWith(this string baseItem, string[] items, string separator = default(string))
        {
            var builder = new StringBuilder();

            Array.ForEach(items,
                item =>
                {
                    builder.Append(value: item).Append(separator);
                });

            return ReferenceEquals(separator, default(string)) ? builder.ToString() : builder.ToString().TrimEnd(separator.ToCharArray());
        }

        public static string JoinWithDifferentTypes<T1, T2, T3>(this string baseItem, Tuple<T1, T2, T3> tupleOfJoinableItems)
            where T1 : struct
            where T2 : struct
            where T3 : class
        {

            var builder = new StringBuilder();
            builder.Append(baseItem).Append(tupleOfJoinableItems.Item1).Append(tupleOfJoinableItems.Item2);

            return ReferenceEquals(tupleOfJoinableItems.Item3, null) ? builder.ToString() : builder.Append(tupleOfJoinableItems.Item3.ToString()).ToString();
        }
}


In the above code, I used string.Join and StringBuilder to join with given strings and also I wrote extension named JoinWithDifferentTypes by which I tried to use multiple types to join into one string item. In the JoinWithDifferentTypes method, I could also use:

return string.Format("{0}{1}{2}{3}", baseItem, tupleOfJoinableItems.Item1, tupleOfJoinableItems.Item2, tupleOfJoinableItems.Item3);


to describe the usage of the above extension methods I used following code blocks:

C#
static void Main(string[] args)
{
            string[] items = new string[] { "One-", "Two-", "Three" };
            string baseItem = "Zero";

            Console.WriteLine("Results :\n {0}\n {1}\n {2}\n {3}\n {4}\n {5}\n {6}\n {7}",
                new object[]
                {
                    baseItem.JoinWithSeparator("One", Definitions.Separator),
                    baseItem.JoinWithSeparator("One", "Two", Definitions.Separator),
                    baseItem.JoinWithSeparator(new[] { "One", "Two" }, Definitions.Separator),
                    baseItem.JoinWith(Definitions.Topics.Split(new[] { ',' }), Definitions.Separator),
                    baseItem.JoinWith(Definitions.Topics.Split(new[] { ',' })),
                    baseItem.JoinWithDifferentTypes<int, char, object>(new Tuple<int, char, object>(1, 'C', null)),
                    baseItem.JoinWithDifferentTypes<int, char, Person>(new Tuple<int, char, Person>(1, 'C', new Person() { Name = "Name", Address = "Address" }))
                });
            Console.ReadKey();
}


JoinWithSeparator

There are three overloaded versions and all of those have a common behavior which is the separator and it is optional. Besides that, if I want to join with a single item, I could use JoinWithSeparator extension for example, "JoinWith".JoinWithSeparator("Me") or if want to add two items with separator, then "JoinWith".JoinWithSeparator("A","B",Definitions.Separator). This extension is preferable for the circumstances where we need to add One or Two items with baseItem.

JoinWith

The JoinWith extension will take an array of string tokens and will add those with the baseitem. If we need to add many items, then we should use this extension.

JoinWithDifferentTypes

If we want to add different types of items into the baseItem (which is string in here), then we can use this one.

and some other related classes I used:

C#
public class Person
{
    public string Name { get; set; }
    public string Address { get; set; }

    public override string ToString()
    {
       return Name.JoinWith(Address);
    }
}

public class Definitions
{
    public const string
           Separator = "-",
           Topics = "Programming, Music, Documentary";
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Australia Australia

Comments and Discussions

 
GeneralRe: Well, hoorah for the compiler! I just checked and string + s... Pin
cameronsstone22-Jun-11 15:19
cameronsstone22-Jun-11 15:19 
GeneralHi there, it is good idea too. Thanks Pin
Mohammad A Rahman20-Jun-11 22:40
Mohammad A Rahman20-Jun-11 22:40 
GeneralRather than the multiple JoinWithSeparator methods, why not ... Pin
REEDY83720-Jun-11 22:06
REEDY83720-Jun-11 22:06 
Rather than the multiple JoinWithSeparator methods, why not just use params:

public static string JoinWithSeparator(this string baseItem, string separator = default(string), params string[] items)
{
return string.Join(separator, new[] { baseItem, string.Join(separator, items) });
}
GeneralJust checked what you said about string.Concat and I was hor... Pin
cameronsstone1-Jun-11 18:42
cameronsstone1-Jun-11 18:42 
GeneralRe: Don't be too horrified by string.Concat using + to concatena... Pin
Jim Horvath22-Jun-11 14:15
Jim Horvath22-Jun-11 14:15 
GeneralThank you so much...... Pin
Pritesh Aryan23-May-11 23:58
Pritesh Aryan23-May-11 23:58 
GeneralRe: happy to know, it helps. Pin
Mohammad A Rahman24-May-11 0:12
Mohammad A Rahman24-May-11 0:12 
Generalin below code &lt;pre&gt;public static string JoinWithSepara... Pin
Pritesh Aryan23-May-11 20:05
Pritesh Aryan23-May-11 20:05 
GeneralRe: Hi there, new[]{...} its Implicit Typed Array. From the meth... Pin
Mohammad A Rahman23-May-11 22:26
Mohammad A Rahman23-May-11 22:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.