by Sai gowtham

How to split a string using the string delimiter in C#

We can split a string with a string delimiter by using the String.Split() method in C#.

Here is an example that splits the string with a string awesome:

using System;
class SplitStringProgram {
  static void Main() {
      string msg = "This is big awesome quote from einstein";
      string[] arr = msg.Split(new[] { "awesome" }, StringSplitOptions.None);
       foreach (string val in arr) {
         Console.WriteLine(val);
      }
  }
}

Output:

This is big
quote from einstein

If a string delimiter contains a single character like , or ; we can split it by just passing character to the Split() method.

using System;
class SplitStringProgram {
  static void Main() {
      string msg = "Welcome, to, the programming world";
      string[] arr = msg.Split(',');
       foreach (string val in arr) {
         Console.WriteLine(val);
      }
  }
}

Output:

Welcome
to
the programming world

Top Udemy Courses

JavaScript - The Complete Guide 2020 (Beginner + Advanced)
JavaScript - The Complete Guide 2020 (Beginner + Advanced)
45,614 students enrolled
52 hours of video content
View Course
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
284,472 students enrolled
40 hours of video content
View Course
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
130,921 students enrolled
21 hours of video content
View Course