Techiehook Techiehook
Updated date Nov 23, 2024
This program provides how to split a string in C#, providing various methods such as the Split() method, Substring() method, regular expressions, StringSplitOptions, splitting a string into an array of characters, and splitting a string using multiple delimiters.

How to Split a string in C#:

We can use multiple ways to split the string in C#, we will go through the methods one by one in this article.

1. Split() Method:

The Split() method is a built-in method in C# that is used to split a string into an array of substrings based on a specified delimiter. It also returns an array of strings.

Syntax:

string[] words = str.Split(char[] separator, StringSplitOptions options)

Here, the separator parameter is a character array that contains the delimiters. The options parameter is an enumeration that specifies whether to include empty substrings in the array.

Example:

string str = "This is a sample string";
char[] separator = { ' ' };
string[] words = str.Split(separator, StringSplitOptions.RemoveEmptyEntries);
foreach (string word in words)
{
   Console.WriteLine(word);
}

Output:

This
is
a
sample
string

In this example, we split the string "This is a sample string" based on the space character (' ') delimiter.

2. Substring() Method:

The Substring() method is another built-in method in C# that is used to extract a substring from a given string. We can use this method to split a string into smaller parts.

Syntax:

string str = "This is a sample string";
string subStr = str.Substring(int startIndex, int length);

The startIndex parameter is the zero-based starting index of the substring, and the length parameter is the number of characters to extract.

Example:

string str = "This is a sample string";
string subStr1 = str.Substring(0, 4);
string subStr2 = str.Substring(5, 2);
string subStr3 = str.Substring(8, 1);
string subStr4 = str.Substring(10, 6);
string subStr5 = str.Substring(17, 6);

Console.WriteLine(subStr1);
Console.WriteLine(subStr2);
Console.WriteLine(subStr3);
Console.WriteLine(subStr4);
Console.WriteLine(subStr5);

Output:

This
is
a
sample
string

In this example, we used the Substring() method to extract substrings from the original string "This is a sample string" based on the starting index and length parameters.

3. Regular Expressions:

Regular expressions are a powerful tool for manipulating text in C#. We can use regular expressions to split a string based on a specific pattern.

Syntax:

using System.Text.RegularExpressions;

string str = "This is a sample string";
string[] words = Regex.Split(str, string pattern);

The pattern parameter is a regular expression pattern that specifies the delimiters.

Example:

using System.Text.RegularExpressions;

string str = "This is a sample string";
string[] words = Regex.Split(str, @"\s+");
foreach (string word in words)
{
   Console.WriteLine(word);
}

Output:

This
is
a
sample
string

In this example, we used the regular expression pattern @"\s+" to split the string "This is a sample string" based on one or more whitespace characters.

4. StringSplitOptions.RemoveEmptyEntries:

The StringSplitOptions is an enumeration that provides options for the Split() method. This enumeration has two possible values: None and RemoveEmptyEntries.

  1. The None option returns all substrings, including empty substrings.
  2. The RemoveEmptyEntries option removes empty substrings from the array.

Example:

string str = "This is a  sample  string";
char[] separator = { ' ' };
string[] words = str.Split(separator, StringSplitOptions.None);
foreach (string word in words)
{
   Console.WriteLine(word);
}

Output:

This
is
a

sample

string

In this example, we used the Split() method with the None option. The method returned an array of substrings that included empty substrings.

5. Splitting a string into an array of characters:

We can also split a string into an array of characters using the ToCharArray() method.

Syntax:

string str = "This is a sample string";
char[] charArray = str.ToCharArray();

Example:

string str = "This is a sample string";
char[] charArray = str.ToCharArray();
foreach (char c in charArray)
{
   Console.WriteLine(c);
}

Output:

T
h
i
s

i
s

a

s
a
m
p
l
e

s
t
r
i
n
g

In this example, we used the ToCharArray() method to split the string "This is a sample string" into an array of characters.

6. Splitting a string using multiple delimiters:

We can split a string using multiple delimiters by using the Split() method with an array of delimiters.

Example:

string str = "This, is, a, sample, string";
char[] separator = { ',', ' ' };
string[] words = str.Split(separator, StringSplitOptions.RemoveEmptyEntries);
foreach (string word in words)
{
   Console.WriteLine(word);
}

Output:

This
is
a
sample
string

In this example, we split the string "This, is, a, sample, string" using both the comma and space characters as delimiters.

ABOUT THE AUTHOR

Techiehook
Techiehook
Admin, Australia

Welcome to TechieHook.com! We are all about tech, lifestyle, and more. As the site admin, I share articles on programming, tech trends, daily life, and reviews... For more detailed information, please check out the user profile

https://www.techiehook.com/profile/alagu-mano-sabari-m

Comments (0)

There are no comments. Be the first to comment!!!