How to split a string with a string delimiter in C#?


Delimiters are the commas that you can see in the below string.

string str = "Welcome,to,New York";

Now set the delimiter separately.

char[] newDelimiter = new char[] { ',' };

Use theSplit() method to split the string considering the delimiter as the parameter.

str.Split(newDelimiter, StringSplitOptions.None);

To split a string with a string deli meter, try to run the following code −

Example

 Live Demo

using System;
class Program {
   static void Main() {
      string str = "Welcome,to,New York";
      char[] newDelimiter = new char[] { ',' };
      string[] arr = str.Split(newDelimiter, StringSplitOptions.None);
      foreach (string val in arr) {
         Console.WriteLine(val);
      }
   }
}

Output

Welcome
to
New York

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 23-Jun-2020

623 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements