
C#字符串Split()简介
用于将一个给定的字符串分割成一个字符串数组,该方法被称为C# String Split()方法,分割的分界线是一个由字符串组成的数组或由字符组成的数组或只是字符,由子字符串组成的数组,其分界线是Unicode字符数组或指定字符串的元素,在C#中使用Split()方法会返回ArgumentOutofRangeException和ArgumentException异常,作为使用此方法时异常处理的一部分。
语法
C#字符串Split()方法的语法如下。
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)
其中分离器是给定字符串中限定子串的字符串数组。
count保持要返回的最大子串数量的计数
选项可以删除空条目选项,以便从返回的数组中丢弃空的数组元素,或者选择无选项,以便从返回的数组中包含空的数组元素。
C# String Split()方法的工作原理
- 每当需要根据分隔字符串数组、字符数组或仅仅是字符的分隔符来分割字符串时,我们就会使用String split()方法。
- 分隔字符串的定界符可以是字符串数组或字符数组,也可以只是字符。
- 使用string split()方法可以返回基于给定分隔符的字符串的子串。
- 在使用Split()方法时,会引发ArgumentOutofRangeException和ArgumentException异常作为异常处理的一部分。
C#字符串Split()的例子
下面是C#字符串Split()的例子。
例子#1
演示String Split()方法的C#程序,以分隔由逗号分隔的字符串
代码。
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); } } }
输出。

在上面的程序中,调用了一个叫做check的类。然后调用main方法,在该方法中定义了一个字符串变量来存储由分隔符组成的字符串,这些分隔符可以用来将给定的字符串分离成一个子字符串数组,然后定义了一个字符串数组来存储通过使用string split()方法返回的子字符串数组,该数组被显示为输出。输出结果显示在上面的快照中。
示例#2
C#程序演示string split()方法将由多个分隔符组成的给定字符串分离成一个字符串数组。
代码。
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(); } } }
输出。

在上面的程序中,创建了一个名为program的命名空间。然后定义了一个名为check的类,并在其中调用了main方法。然后定义了一个字符串变量来存储由多个定界符组成的字符串。然后我们使用split()方法将给定的字符串分离成一个子字符串数组,显示为输出。输出结果显示在上面的快照中。
示例#3
C#程序演示了String IndexOf()方法来分离由多个定界符组成的给定字符串,并将返回值存储在一个列表中。
代码。
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(); } } }
输出。

在上面的程序中,创建了一个名为program的命名空间。然后定义了一个名为check的类,并在其中调用了main方法。然后定义了一个字符串变量来存储由多个分隔符组成的字符串。然后,通过使用字符串split()方法,给定的字符串可以通过创建一个新的列表被分割成一个存储在列表中的字符串阵列。输出结果显示在上面的快照中。