Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c#
split
string

What is the Split method in C#?

Educative Answers Team

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code

The Split() method is part of the string class in C#. The method is used to split a string based on the delimiters passed to the string.

The delimiters can be a character, an array of characters, or even an array of strings. We can also pass the function an array of strings to be split on the delimiters passed to it.

svg viewer

Code

The following code illustrates the various ways that the split() function can be used. Note the various type of arguments passed to the function.

1. Splitting a string on a character

using System;
class Program
{
static void Main()
{
string sentence = "czechoslovakia";
string[] words = sentence.Split('o');
foreach (string word in words)
{
Console.WriteLine(word);
}
}
}

2. Splitting a string on an array of characters

using System;
class Program
{
static void Main()
{
string sentence = "the quick:brown fox";
char[] separator = { ':', ' ' };
string[] words = sentence.Split(separator);
foreach (string word in words)
{
Console.WriteLine(word);
}
}
}

3. Splitting a string on an array of strings

using System;
class Program
{
static void Main()
{
string[] separator = { "<<", "..." };
string sentence = "one<<two......three<four";
string[] words = sentence.Split(separator, System.StringSplitOptions.RemoveEmptyEntries);
foreach (var word in words)
{
System.Console.WriteLine(word);
}
}
}

RELATED TAGS

c#
split
string
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code
Keep Exploring