只要我们了解上面的四种参数分别代表什么意思,我们就可以根据我们的需求去具体的选择我们需要重载哪一个Split函数。下面是我将所有的Split函数重载方法实现的一个小例子:
public string[] Split(params char[] separator);
class Program { static void Main(string[] args) { string s1 = "所属机构名称/教师姓名/课程类型/课程名称"; string[] split = s1.Split(new char[] { '/' }); //返回由'/'分隔的子字符串数组 foreach (string s in split) { Console.WriteLine(s); } Console.WriteLine(); } }
public string[] Split(char[] separator, int count);
class Program { static void Main(string[] args) { string s1 = "所属机构名称/教师姓名/课程类型/课程名称"; string[] split = s1.Split(new char[] { '/' }, 3); //返回由'/'分隔的子字符串数组,子字符串最大数量为3个 foreach (string s in split) { Console.WriteLine(s); } Console.WriteLine(); } }
public string[] Split(char[] separator, StringSplitOptions options);
class Program { static void Main(string[] args) { string s1 = "所属机构名称/教师姓名/课程类型/课程名称"; //返回由'/'分隔的子字符串数组,StringSplitOptions.RemoveEmptyEntries表示返回值不包括含有空字符串的数组元素 string[] split = s1.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries); foreach (string s in split) { Console.WriteLine(s); } Console.WriteLine(); } }
public string[] Split(string[] separator, StringSplitOptions options);
class Program { static void Main(string[] args) { string s1 = "所属机构名称/教师姓名/课程类型/课程名称"; //返回由"名"分隔的子字符串数组,StringSplitOptions.None表示返回值包括含有空字符串的数组元素 string[] a = new string[] { "名" }; string[] split = s1.Split(a, StringSplitOptions.None); foreach (string s in split) { Console.WriteLine(s); } Console.WriteLine(); } }
public string[] Split(char[] separator, int count, StringSplitOptions options);
class Program { static void Main(string[] args) { string s1 = "所属机构名称/教师姓名/课程类型/课程名称"; //返回由'/'分隔的子字符串数组,子字符串最大数量为3个,StringSplitOptions.RemoveEmptyEntries表示返回值不包括含有空字符串的数组元素 string[] split = s1.Split(new char[] { '/' }, 3, StringSplitOptions.RemoveEmptyEntries); foreach (string s in split) { Console.WriteLine(s); } Console.WriteLine(); } }
public string[] Split(string[] separator, int count, StringSplitOptions options);
class Program { static void Main(string[] args) { string s1 = "所属机构名称/教师姓名/课程类型/课程名称"; //返回由"名"分隔的子字符串数组,子字符串最大数量为3个,StringSplitOptions.None表示返回值包括含有空字符串的数组元素 string[] a = new string[] { "名" }; string[] split = s1.Split(new char[] { '/' }, 3, StringSplitOptions.None); foreach (string s in split) { Console.WriteLine(s); } Console.WriteLine(); } }
class Program { static void Main(string[] args) { string s1 = "所属机构名称/教师姓名/课程类型/课程名称"; string[] split = s1.Split(new char[] { '/' }); //返回由'/'分隔的子字符串数组 foreach (string s in split) { Console.WriteLine(s); } Console.WriteLine(); } }
原文:http://blog.csdn.net/ry513705618/article/details/42218275