Part 35 String Join()

String Join() : string join() method is concatenate all the string . i.e combine string elements from an array into a new single string.

String.Join() total five overloaded Join() method.


  • String.Join(String, Obj [ ])
  • String.Join(String, string [ ])
  • String.Join(String, string [ ], int Index, int Index1)
  • String.Join(String, IEnumerable<String>)
  • String.Join<T>(String, IEnumerable<T>)
1. String.Join(String, Obj [ ]) 


string join() method is used concatenate the string using to object[], number of element in the array store in the Object[] and concatenate the string. 

class Program

    {
        {
            object[ ] source = new[ ]   { "CSharp", "Tutorial", "Point" };
            string s1 = string.Join("- ", source);
            System.Console.WriteLine(s1);
            System.Console.ReadLine();
        }
    }
Output :
CSharp-Tutorial-Point
    2.   String.Join(String, string [ ])
    string join() method is used concatenate the string using to string[], number of element in the array store in the string[] and concatenate the string. 


     class Program

        {
            {

                string  [] array= { "CSharp", "Tutorial", "Point" };

                string  s1 = string.Join("-", array);
                System.Console.WriteLine(s1);
                System.Console.ReadLine();

            }

        }

    Output :
    CSharp-Tutorial-Point

    3.  String.Join(String, string [ ], int Index, int Index1)

    string join() method is used concatenate the string using to string[], number of element in the array store in the string[] and String array the index positions with the help of a user-defined separator between each element of the array. 

    class Program
        {
            {
                string[] array = {"Chandan","csharp", "tutorial", "point", "blogspot"  };
                // from index 1 and covers upto 4  
                // elements from index 2 
                string s1 = string.Join("-", array, 1, 4);
                Console.WriteLine(s1);
                Console.ReadLine();
            }
        }
    Output :
    csharp-tutorial-point-blogspot

    4.  String.Join(String, IEnumerable<String>)

     class Program
        {
            static void Main(string[] args)
            { 
                List<string> data = Additems();  
                string str1 = string.Join("-", data);
                System.Console.WriteLine("Are you learning from " + str1);
                System.Console.ReadLine();
            }
            private static List<string> Additems()
            {
                List<string> learn = new List<string>();
                learn.Add("csharp");
                learn.Add("tutorial");
                learn.Add("point");
                learn.Add("blogspot");
                learn.Add("com");
                return learn;
            }
        }


    Output :
    Are you learning from csharp-tutorial-point-blogspot-com

    5.  String.Join<T>(String, IEnumerable<T>)


    This method is used to concatenate the constructed collection of any user defined data type.


     public  class tutorial
        {
            public string tutorialwebsite;
            // constructor to hold the  
            // string values of item class 
            public tutorial(string name1)
            {
                tutorialwebsite = name1;
            }

            public override string ToString()
            {
                return this.tutorialwebsite;
            }
        }

       class Program
        {
            {
                // passing the list of objects 
                List<tutorial> data = Additems();  
                string str1 = string.Join("-", data);
                System.Console.WriteLine("Are you learning from " + str1);
                System.Console.ReadLine();
            }
            private static List<tutorial> Additems()
            {
                // adding the objects of item  
                List<tutorial> learn = new List<tutorial>();
                learn.Add(new tutorial("csharp"));
                learn.Add(new tutorial("tutorial"));
                learn.Add(new tutorial("point"));
                learn.Add(new tutorial("blogspot"));
                learn.Add(new tutorial("com"));
                return learn;
            }
        }



    Output :

    Are you learning from csharp-tutorial-point-blogspot-com