C# program to split and join a string


To split and join a string in C#, use the split() and join() method. Let us say the following is our string −

string str = "This is our Demo String";

To split the string, we will use the split() method −

var arr = str.Split(' ');

Now to join, use the join() method and join rest of the string. Here, we have skipped the part of the string using the skip() method −

string rest = string.Join(" ", arr.Skip(1));

Example

You can try to run the following code in C# to split and join a string.

Live Demo

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Demo {
   class MyApplication {
      static void Main(string[] args) {
         string str = "This is our Demo String";
         var arr = str.Split(' ');
         // skips the first element and joins rest of the array
         string rest = string.Join(" ", arr.Skip(1));
         Console.WriteLine(rest);
      }
   }
}

Output

is our Demo String

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 19-Jun-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements