只要我們了解上面的四種參數分別代表什麼意思,我們就可以根據我們的需求去具體的選擇我們需要重載哪一個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(); } }
<喎?http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PGJyPgo8YnI+CjwvcD4KPHA+cHVibGljIHN0cmluZ1tdIFNwbGl0KGNoYXJbXSBzZXBhcmF0b3IsIGludCBjb3VudCk7PC9wPgo8cD48L3A+CjxwcmUgY2xhc3M9"brush:java;">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(); } }