String - Split
Returns a String array containing the substrings in this string that are delimited by elements of a specified String array. A parameter specifies whether to return empty array elements.
public static string FileName = "test.txt"; public static void Main() { string phrase = "The quick brown fox jumps over the lazy dog."; //C# Extension Method: String - Split string[] words = phrase.Split(" ", StringSplitOptions.RemoveEmptyEntries); foreach (var word in words) { System.Console.WriteLine(word); } }
View Source
using System; public static partial class Extensions { /// <summary> /// Returns a String array containing the substrings in this string that are delimited by elements of a specified /// String array. A parameter specifies whether to return empty array elements. /// </summary> /// <param name="this">The @this to act on.</param> /// <param name="separator">A string that delimit the substrings in this string.</param> /// <param name="option"> /// (Optional) Specify RemoveEmptyEntries to omit empty array elements from the array returned, /// or None to include empty array elements in the array returned. /// </param> /// <returns> /// An array whose elements contain the substrings in this string that are delimited by the separator. /// </returns> public static string[] Split(this string @this, string separator, StringSplitOptions option = StringSplitOptions.None) { return @this.Split(new[] {separator}, option); } }