In C#, Split() is a string class method used to divide a string into an array of substrings. The Split() method divides a string into an array of substrings using specified delimiters.
The delimiters can be:
- A character for example comma(,) or underscore(-).
- An array of characters char[] or an array of strings string[].
In simple words, it returns a string array that contains the substrings in the current instance that are delimited by elements of a specified string or Unicode character array.
Example: Separating a string based on space using String.Split() method.
C#
// C# program to illustrate the
// String.Split() Method
using System;
class Geeks
{
public static void Main()
{
// declaring the string
String str = "Hello from GeeksforGeeks";
// Splitting the string
// into substrings separated by spaces
string[] split = str.Split(' ');
foreach (var word in split)
{
Console.WriteLine(word);
}
}
}
OutputHello
from
GeeksforGeeks
Explanation: In the above example, we use the Split() method which converts the string into the string array based on a single space and then we print each string present in the array using the foreach loop.
Different Overloads of String.Split() Method
These are the six overloads of String.Split() method.
Methods | Description |
---|
Split(String[], Int32, StringSplitOptions) | Split string into a maximum number of sub-strings based on the array of strings passed as parameters. We can specify whether to include the empty array elements in an array of substrings or not. |
---|
Split(Char[], Int32, StringSplitOptions) | Split string into a maximum number of sub-strings based on the array of characters passed as a parameter. We can specify whether to include the empty array elements in an array of substrings or not. |
---|
Split(String[], StringSplitOptions) | Splits a string into substrings based on the array of strings. We can specify whether to include the empty array elements in an array of substrings or not. |
---|
Split(Char[]) | Splits a string into substrings based on the array of characters. |
---|
Split(Char[], StringSplitOptions) | Splits a string into substrings based on the array of characters. We can specify whether to include the empty array elements in an array of substrings or not. |
---|
Split(Char[], Int32) | Split string into a maximum number of sub-strings based on the array of characters passed as a parameter. We can specify a maximum number of sub-strings to return. |
---|
Parameters:
- separator: It is a character or a set of characters or an array of string[] that defines the points at which a string should be split into substrings
- options: RemoveEmptyEntries or None option to omit empty array elements from the array returned or None option to include empty array elements in the array returned.
- count: It defines the maximum number of substrings to return. If omitted, all possible substrings are returned.
Return Type: This method returns an array whose elements contain the substrings in this string which are delimited by one or more characters or an array of strings in the separator .
Exceptions:
- ArgumentOutOfRangeException: If the count is negative.
- ArgumentException: If the options are not one of the StringSplitsOptions values.
1. Split(String[], Int32, StringSplitOptions)
This method is used to split a string into a maximum number of substrings based on the strings in an array. We can specify whether the substrings include empty array elements or not.
Syntax:
public String[] Split(String[] separator, int count, StringSplitOptions options);
Example: Separate the string using the Split(String[], Int32, StringSplitOptions) method.
C#
// C# program to illustrate Split(String[], Int32, StringSplitOptions)
using System;
class Geeks
{
static void Main(string[] args)
{
// Declaring the string
string s = "Geeks, For Geeks";
// String array of separators
string[] sp = { ",", "For" };
// Count of substrings to be returned
int c = 2;
// Splitting the string
string[] strlist = s.Split(sp, c, StringSplitOptions.RemoveEmptyEntries);
// Displaying the substrings
foreach(string str in strlist)
{
Console.WriteLine(str);
}
}
}
2. Split(Char[], Int32, StringSplitOptions)
This method is used to split a string into a maximum number of substrings based on the characters in an array passed on the argument and also we can pass the count of the maximum substring to return.
Syntax:
public String[] Split(char[] separator, int count, StringSplitOptions options);
Example:
C#
// C# program to illustrate the
// Split(Char[], Int32,
// StringSplitOptions) Method
using System;
class Geeks
{
static void Main(string[] args)
{
// Taking a string
String str = "Geeks, For Geeks";
// Array of spliters
char[] sp = { ',', ' ' };
// Count of substrings
// to be returned
Int32 c = 2;
// Using the split method
// with StringSplitOptions.None
String[] list = str.Split(sp,
c, StringSplitOptions.None);
foreach(String s in list)
Console.WriteLine(s);
}
}
3. Split(String[], StringSplitOptions)
This method is used to split a string into substrings based on the strings in an array. We can also specify whether the substrings include empty array elements or not.
Syntax:
public String[] Split(String[] separator, StringSplitOptions options);
Example:
C#
// C# program to illustrate the
// Split(String[], StringSplitOptions) Method
using System;
class Geeks
{
static void Main(string[] args)
{
// Declaring and initializing the string
String str = "Geeks,For Geeks";
// Array of separators
String[] sp = { ",", " " };
// Splitting the string
String[] list = str.Split(sp, StringSplitOptions.None);
foreach(String s in list)
{
Console.WriteLine(s);
}
}
}
4. Split(char[])
This method is used to split a string into a maximum number of substrings based on the characters in an array. We can also specify the maximum number of substrings to return.
Syntax:
public String[] Split(char[] separator);
Example:
C#
// C# program to illustrate the
// Split(Char[]) Method
using System;
class Geeks
{
static void Main(string[] args)
{
// Declaring and initializing the string
String str = "Geeks,For Geeks";
// Character array of separator
// to split the string
char[] separator = { ',', ' ' };
// Using the split method
String[] strlist = str.Split(separator);
foreach(String s in strlist)
{
Console.WriteLine(s);
}
}
}
5. Split(char[], StringSplitOptions )
This method splits a string into substrings based on the characters in an array passed as a separator. We can also specify whether empty array elements should be included or not using the second parameter StringSplitOptions.
Syntax:
public String[] Split(char[] separator, StringSplitOptions option);
Example:
C#
// C# program to illustrate the use of
// Split(Char[], StringSplitOptions) method
using System;
class Geeks
{
static void Main(string[] args)
{
String s = "Geeks, For Geeks";
// Character array as
// As a separator
char[] sp = { ',', ' ' };
// Using the method
String[] list = s.Split(sp,
StringSplitOptions.RemoveEmptyEntries);
foreach(String i in list)
{
Console.WriteLine(i);
}
}
}
6. Split(Char[], Int32)
This method is used to splits the string into a maximum number of substrings based on the characters in an array passed as a seperator. We can also specify the maximum number of substrings which we want to return.
public String[] Split(char[] separator, int count);
Example:
C#
// C# program to illustrate Split(Char[], Int32)
using System;
class Geeks
{
static void Main(string[] args)
{
// Declaring and initializing the string
string s = "Geeks,For Geeks";
// Character array of separators
char[] sp = { ',', ' ' };
// Using the split method
string[] list = s.Split(sp, 3);
foreach(string i in list)
{
Console.WriteLine(i);
}
}
}
Similar Reads
C# Tutorial C# (pronounced "C-sharp") is a modern, versatile, object-oriented programming language developed by Microsoft in 2000 that runs on the .NET Framework. Whether you're creating Windows applications, diving into Unity game development, or working on enterprise solutions, C# is one of the top choices fo
4 min read
Introduction to .NET Framework The .NET Framework is a software development framework developed by Microsoft that provides a runtime environment and a set of libraries and tools for building and running applications on Windows operating systems. The .NET framework is primarily used on Windows, while .NET Core (which evolved into
6 min read
C# Interview Questions and Answers C# is the most popular general-purpose programming language and was developed by Microsoft in 2000, renowned for its robustness, flexibility, and extensive application range. It is simple and has an object-oriented programming concept that can be used for creating different types of applications.Her
15+ min read
C# Dictionary Dictionary in C# is a generic collection that stores key-value pairs. The working of Dictionary is quite similar to the non-generic hashtable. The advantage of a Dictionary is, that it is a generic type. A dictionary is defined under System.Collections.Generic namespace. It is dynamic in nature mean
5 min read
C# List Class In C#, the List<T> class represents the list of objects that can be accessed by index. It comes under the System.Collections.Generic namespace. List class can be used to create a collection of different types like integers, strings, etc. List<T> class also provides the methods to search,
7 min read
C# Delegates A delegate is an object which refers to a method or you can say it is a reference type variable that can hold a reference to the methods. It provides a way which tells which method is to be called when an event is triggered. For example, if you click on a Button on a form (Windows Form application),
6 min read
ASP.NET Interview Questions and Answer ASP.NET is a popular framework by Microsoft for building fast and scalable web applications. It allows developers to create dynamic websites, services, and apps, using server-side code and offering a user-friendly experience. Trusted by companies like Microsoft, Dell, and Accenture, ASP.NET is used
15+ min read
C# .NET Framework (Basic Architecture and Component Stack) C# (C-Sharp) is a modern, object-oriented programming language developed by Microsoft in 2000. It is a part of the .NET ecosystem and is widely used for building desktop, web, mobile, cloud, and enterprise applications. This is originally tied to the .NET Framework, C# has evolved to be the primary
6 min read
C# Data Types Data types specify the type of data that a valid C# variable can hold. C# is a strongly typed programming language because in C# each type of data (such as integer, character, float, and so forth) is predefined as part of the programming language and all constants or variables defined for a given pr
7 min read
C# Arrays An array is a group of like-typed variables that are referred to by a common name. And each data item is called an element of the array. The data types of the elements may be any valid data type like char, int, float, etc. and the elements are stored in a contiguous location. Length of the array spe
8 min read