Part 27 String Join() & String.Format()

String join() is a c# string method. This join() method is used to concatenates the members of all elements of the specified array, The specified separator between each member or element. These are the following  string Join() .

  1. public static string Joint(String, params Object[] arr)  
  2. public static string Join(String first, params String[] arr) 
  3. public static string Join (String, IEnumerable<String>) 
  4. public static string Join(String, String[], Int32, Int32)  
  5. public static string Join<T>(String, IEnumerable <T>)  


    1. public static string Join(String first, params object[] arr)  


    This method takes two parameter , First of them is system.string which is used to define a separator,another parameter is an array of type Params which contain the element. 


    Example :Append or concatenate all string in an array.It return modify string

    class Program
     {


          static string CombineA(params object[] arr)

          {

                   return string.Join("-", arr);
           }


           {

                string[] data= { "Csharp", "tutorial", "point", "blogspot" };

              // Passing arguments of variable length 
                // CombineA: use method that calls string.Join.

            Console.WriteLine(CombineA(data));
            Console.ReadLine();
            }
    }
    Output:
    Csharp-tutorial-point-blogspot

    2. public static string Join(String first, String[] arr)  




    class Program
     {



          static string CombineA(params string [] arr)


          {

                   return string.Join("-", arr);
           }


           {

                string[] data= { "Csharp", "tutorial", "point", "blogspot" };

              // Passing arguments of variable length 
                // CombineA: use method that calls string.Join.
            Console.WriteLine(CombineA(data));
            Console.ReadLine();
            }
    }
    Output:
    Csharp-tutorial-point-blogspot

    Example 2:
    class Program
        {
            {
                string[] s1 = { "Csharp", "tutorial", "point", "blogspot" };
    // Join(String, Obj [ ]) method
                string s3 = string.Join("-", s1);
                Console.WriteLine(s3);
                Console.ReadLine();
            }
        }
    Output:
    Csharp-tutorial-point-blogspot



    3. public static string Join (String, IEnumerable<String>) 


    This method is used to concatenates the members of a constructed collection of type String.


    class Program

        {
            static  List<string> csharpjoin()
            {
                List<String> data = new List<string>();
                data.Add("csharp");
                data.Add("tutorial");
                data.Add("point");
                return data;
            }
            static void Main(string[] args)
            {           
                string val = string .Join("-", csharpjoin());
                Console.WriteLine(val);
                Console.ReadLine();
            }
            

        }
    Example 2: 

    class Program
        {
            {
                 string list = new List<string>()  { "Csharp", "tutorial", "point", "blogspot" };
    // Join the strings from the List.
    string joined = string.Join<string>("*", list);
    // Display.
     Console.WriteLine(joined);
                Console.ReadLine();
            }
        }
    Output
    Csharp-tutorial-point-blogspot



    Example 4
    class Program
        {
            {
                 // combine these words
    string [] str= new string [] { "Csharp", "tutorial", "point", "blogspot" };
    // Solution: join with break tag. string data= string.Join("<br/>\r\n", str);  Console.WriteLine(data);
               Console.ReadLine();
            }
        }

    5. public static string Join<T>(String, IEnumerable <T>)  
    This method is used to concatenates the members of a collection  user defined data type 

    Note: The  IEnumerable <T> can give ArgumentNullException if the T is null.


    namespace csharp_tutorial_point

    {
        public class items
        {
            public string itemname;
            // constructor to hold the  
            // string values of item class 
            public items(string name1)
            {
                itemname = name1;
            }
            public override string ToString()
            {
                return this.itemname;
            }
        }
        class Program
        {
            static List<items> csharpjoin()
            {
                List<items> data = new List<items>();
                data.Add(new items("csharp"));
                data.Add(new items("tutorial"));
                data.Add(new items("point"));
                return data;
            }
             static void Main(string[] args)
            {
                string val = string .Join("-", csharpjoin());
                 Console.WriteLine(val);
                Console.ReadLine();
            }
        }

    }