緣起
程式範例
以下範例來自 MSDN
string source = "[stop]ONE[stop][stop]TWO[stop][stop][stop]THREE[stop][stop]"; string[] stringSeparators = new string[] { "[stop]" }; //建立切割依據的字串陣列 stringSeparators[0] = "[stop]" string[] result; // Display the original string and delimiter string. Console.WriteLine("Splitting the string:\n \"{0}\".", source); Console.WriteLine(); Console.WriteLine("Using the delimiter string:\n \"{0}\"", stringSeparators[0]); Console.WriteLine(); // Split a string delimited by another string and return all elements. //開始進行切割, 切割時, 會將空字串 (Empty or null) 也視為切割後的一個陣列元素 result = source.Split(stringSeparators, StringSplitOptions.None); Console.WriteLine("Result including all elements ({0} elements):", result.Length); Console.Write(" "); foreach (string s in result) //將切割後的字串進行逐一顯示 { Console.Write("'{0}' ", String.IsNullOrEmpty(s) ? "<>" : s); } Console.WriteLine(); Console.WriteLine(); // Split delimited by another string and return all non-empty elements. //開始進行切割, 切割時, 空字串 (Empty or Null) 不會列入陣列元素 result = source.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries); Console.WriteLine("Result including non-empty elements ({0} elements):", result.Length); Console.Write(" "); foreach (string s in result) { Console.Write("'{0}' ", String.IsNullOrEmpty(s) ? "<>" : s); } Console.WriteLine();
沒有留言:
張貼留言