Home > Backend Development > C#.Net Tutorial > C# String Split()

C# String Split()

王林
Release: 2024-09-03 15:17:42
Original
471 people have browsed it

The method used to divide a given string which is separated by the delimiters for split into an array of strings, is called the C# String Split() method, with the delimiters of the split being an array consisting of strings or an array consisting of characters or just characters and an array of strings consisting of the substrings whose delimiters are an array of Unicode characters or elements of the specified string, is returned by using the Split() method in C# and ArgumentOutofRangeException and ArgumentException exceptions are raised as part of exception handling when using this method.

Syntax

The syntax of the C# String Split() method is as follows:

public String[] Split(String[] separator, int count, StringSplitOptions options);
public String[] Split(params Char[] character)
public String[] Split(Char[], Int32)
public String[] Split(Char[], Int32, StringSplitOptions)
public String[] Split(Char[], StringSplitOptions)
public String[] Split(String[], Int32, StringSplitOptions)
public String[] Split(String[], StringSplitOptions)
Copy after login

Where separator is the array of strings delimiting the substrings in the given string

The count keeps count of the maximum number of substrings to be returned

Options can remove the empty entries option to discard the array elements that are empty from the returned array or the options none to include the empty array elements from the returned array.

Working of C# String Split() method

  • Whenever there is a need to divide the string based on the delimiter separating the array of strings or array of characters or just characters, we make use of the String split() method.
  • The delimiters separating the strings can be an array of strings or an array of characters, or just characters.
  • The substring of the given string separated based on the given delimiter is returned by using a string split() method.
  • ArgumentOutofRangeException and ArgumentException exceptions are raised as part of exception handling when using the Split() method.

Examples of C# String Split()

Given below are the examples of C# String Split():

Example #1

C# program to demonstrate String Split() method to separate the string separated by a comma

Code:

using System;
//a class called check is defined
public class check
{
//main method is called
public static void Main(string[] args)
{
//a string variable is used to store the string consisting of delimiters
string str = "Welcome,to,C,Sharp";
//a string array is used to store the array of strings returned by using string split method
string[] str2 = str.Split(',');
foreach (string s in str2)
{
Console.WriteLine(s);
}
}
}
Copy after login

Output:

C# String Split()

In the above program, a class called check is called. Then the main method is called, within which a string variable is defined to store the string consisting of delimiters which can be used to separate the given string into an array of substrings, and then a string array is defined to store the array of substrings returned by using string split() method which is displayed as output. The output is shown in the snapshot above.

Example #2

C# program to demonstrate string split() method to separate the given string consisting of multiple delimiters into an array of strings:

Code:

using System;
//a namespace called program is defined
namespace program
{
//a class called check is defined
class check
{
//main method is called
static void Main(string[] args)
{
//a string variable is used to store the string consisting of multiple delimiters
string str1 = "Welcome,to-C|Sharp";
//a string array is defined to store the array of substrings returned by using the split() method on the given string
string[] str2 = str1.Split(new char[] { ',', '-', '|' }, StringSplitOptions.RemoveEmptyEntries);
for (int j = 0; j < str2.Length; j++)
{
Console.WriteLine(str2[j]);
}
Console.WriteLine("\nThis is how split() method works");
Console.ReadLine();
}
}
}
Copy after login

Output:

C# String Split()

In the above program, a namespace called program is created. Then the class called check is defined within which the main method is called. Then a string variable is defined to store the string consisting of multiple delimiters. Then we use the split() method to separate the given string into an array of substrings displayed as the output. The output is shown in the snapshot above.

Example #3

C# program to demonstrate String IndexOf() method to separate the given string consisting of multiple delimiters and store the return value in a list:

Code:

using System;
using System.Collections.Generic;
//a namespace called program is created
namespace program
{
//a class called check is defined
class check
{
//main method is called
static void Main(string[] args)
{
//a string variable is defined to store the string consisting of multiple delimiters
string str = "Welcome-to,C|Sharp";
//a list is defined to store the substrings separated from the given string consisting of delimiters
IList<string> listname = new List<string>(str.Split(new char[] { '-', ',', '|' }, StringSplitOptions.RemoveEmptyEntries));
for (int j = 0; j < listname.Count; j++)
{
Console.WriteLine(list[j]);
}
Console.WriteLine("\nThis is how split method works");
Console.ReadLine();
}
}
}
Copy after login

Output:

C# String Split()

In the above program, a namespace called program is created. Then a class called check is defined within which the main method is called. Then a string variable is defined to store the string consisting of multiple delimiters. Then, by using a string split() method, the given string can be split into an array of strings stored in a list by creating a new list. The output is shown in the snapshot above.

The above is the detailed content of C# String Split(). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template