String Join

In this chapter you will learn:

  1. Use the Join() method to join strings
  2. Join string by |

Use the Join() method to join strings

Join method from string type is a static method which concatenate an array of string with delimiter.

It does the opposite of Split method.

using System;/*from  j ava2 s  .c o  m*/

class Sample
{
    public static void Main()
    {
        string[] arr = new string[] { "JAVA", "2", "s", ".com" };

        string s = string.Join(" ", arr);

        Console.WriteLine(s);
    }
}

The output:

Join string by |

using System;//from ja va 2  s  .co  m

public class MainClass
{
    public static void Main()
    {
        string[] s = new string[] { "J", "M", "P", "C" };
        string joined = String.Join("|", s);
        Console.WriteLine(joined);
    }
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. How to split a string
  2. Use the Split() method to split strings
  3. Split with space
  4. Split with a list of dividers
  5. Split by char array
  6. Split a string delimited by another string and return 2 non-empty elements
  7. Split string with \\
Home » C# Tutorial » String
string
String creation
Char in string
Compare strings
String equality
String concatanation
String copy
String Join
String split
String Search for Index
String contains
String start with
String insert
String case
Replacing substring
Remove from a string
Substring
Escape Characters
String verbatim
String padding
Switch on String
String trim
String intern
String normalization
Empty String