Use Regex to Split a line with one or more whitespace chars.

Easy way to split a string on one or more whitespace chars.  String.Split() would return empty array elements if more then one consecutive whitespace char.

string[] fields = Split.SplitLine("one two    three  four");
foreach(string s in fields)

// Split class.
using System;
using System.Text.RegularExpressions;

/// <summary>
/// Summary description for Split.
/// </summary>
public class Split
 private static Regex splitRx = new Regex(@"\s+", RegexOptions.Compiled);
 private Split()

 public static string[] SplitLine(string line)
  return splitRx.Split(line);

William Stacey [MVP]

This entry was posted in C#. Bookmark the permalink.

2 Responses to Use Regex to Split a line with one or more whitespace chars.

  1. Unknown says: Dell d400 Battery Compaq tc4200 Battery Compaq tc4400 Battery Compaq nc4200 Battery Acer aspire 5520 Battery Acer aspire 5920 Battery Acer as07b31 Battery Acer as07b32 Battery Acer as07b41 Battery Acer as07b71 Battery Acer as07b51 Battery Acer batecq60 Battery Acer aspire 1800 Battery Sony vgp-bps2c Battery Sony vgp-bps2a Battery Sony vgp-bps2b Battery Sony vgp-bps2 Battery Acer btp-63d1 Battery Hp dv2000 Battery Hp dv6000 Battery Acer aspire 3610 Battery Acer squ-401 Battery Acer lcbtp03003 Battery Acer batbl50l6 Battery Toshiba pa3399u-1brs Battery Toshiba pa3399u-2bas Battery Acer aspire 5100 series Battery Acer travelmate 4200 series Battery Toshiba pa3421u-1brs Battery Toshiba pa3451u-1brs Battery Toshiba pa3356u Battery Toshiba pa3356u-1bas Battery Toshiba pa3356u-1brs Battery Toshiba pa3356u-2brs Battery Apple a1078 Battery Apple a1175 Battery Apple 15 inch macbook pro Battery Dell 1501 Battery Apple a1185 Battery Apple a1185 white Battery Apple a1185 Battery Apple a1185 black Battery Asus a42-v6 Battery Asus a42-w1 Battery Asus w1000 Battery

  2. Unknown says:

    Serena Williams and the season of her contentwow gold,world of warcraft gold,buy wow gold,wow power leveling,cheap wow gold,world of warcraft power
    leveling,world of warcraft gold,buy wow
    gold,ffxi gil,buy
    wow gold,wow power
    leveling,ffxi gil,world
    of warcraft power leveling,sell wow gold,wow power
    level,wow gold for
    sale,power leveling,wow
    power level,wow power level,power leveling,wow gold for
    sale,buy cheap wow gold.Indian brides confront gold bears, again

Comments are closed.