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");
Console.WriteLine("\nFields:");
foreach(string s in fields)
{
 Console.WriteLine("[{0}]",s);
}

// 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]

Advertisements
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:

    http://www.batteryfast.co.uk/dell/d400.htm Dell d400 Battery http://www.batteryfast.co.uk/compaq/tc4200.htm Compaq tc4200 Battery http://www.batteryfast.co.uk/compaq/tc4400.htm Compaq tc4400 Battery http://www.batteryfast.co.uk/compaq/nc4200.htm Compaq nc4200 Battery http://www.batteryfast.co.uk/acer/aspire-5520.htm Acer aspire 5520 Battery http://www.batteryfast.co.uk/acer/aspire-5920.htm Acer aspire 5920 Battery http://www.batteryfast.co.uk/acer/as07b31.htm Acer as07b31 Battery http://www.batteryfast.co.uk/acer/as07b32.htm Acer as07b32 Battery http://www.batteryfast.co.uk/acer/as07b41.htm Acer as07b41 Battery http://www.batteryfast.co.uk/acer/as07b71.htm Acer as07b71 Battery http://www.batteryfast.co.uk/acer/as07b51.htm Acer as07b51 Battery http://www.batteryfast.co.uk/acer/batecq60.htm Acer batecq60 Battery http://www.batteryfast.co.uk/acer/aspire-1800.htm Acer aspire 1800 Battery http://www.batteryfast.co.uk/sony/vgp-bps2c.htm Sony vgp-bps2c Battery http://www.batteryfast.co.uk/sony/vgp-bps2a.htm Sony vgp-bps2a Battery http://www.batteryfast.co.uk/sony/vgp-bps2b.htm Sony vgp-bps2b Battery http://www.batteryfast.co.uk/sony/vgp-bps2.htm Sony vgp-bps2 Battery http://www.batteryfast.co.uk/acer/btp-63d1.htm Acer btp-63d1 Battery http://www.batteryfast.co.uk/hp/dv2000.htm Hp dv2000 Battery http://www.batteryfast.co.uk/hp/dv6000.htm Hp dv6000 Battery http://www.batteryfast.co.uk/acer/aspire-3610.htm Acer aspire 3610 Battery http://www.batteryfast.co.uk/acer/squ-401.htm Acer squ-401 Battery http://www.batteryfast.co.uk/acer/lcbtp03003.htm Acer lcbtp03003 Battery http://www.batteryfast.co.uk/acer/batbl50l6.htm Acer batbl50l6 Battery http://www.batteryfast.co.uk/toshiba/pa3399u-1brs.htm Toshiba pa3399u-1brs Battery http://www.batteryfast.co.uk/toshiba/pa3399u-2bas.htm Toshiba pa3399u-2bas Battery http://www.batteryfast.co.uk/acer/aspire-5100-series.htm Acer aspire 5100 series Battery http://www.batteryfast.co.uk/acer/travelmate-4200-series.htm Acer travelmate 4200 series Battery http://www.batteryfast.co.uk/toshiba/pa3421u-1brs.htm Toshiba pa3421u-1brs Battery http://www.batteryfast.co.uk/toshiba/pa3451u-1brs.htm Toshiba pa3451u-1brs Battery http://www.batteryfast.co.uk/toshiba/pa3356u.htm Toshiba pa3356u Battery http://www.batteryfast.co.uk/toshiba/pa3356u-1bas.htm Toshiba pa3356u-1bas Battery http://www.batteryfast.co.uk/toshiba/pa3356u-1brs.htm Toshiba pa3356u-1brs Battery http://www.batteryfast.co.uk/toshiba/pa3356u-2brs.htm Toshiba pa3356u-2brs Battery http://www.batteryfast.co.uk/apple/a1078.htm Apple a1078 Battery http://www.batteryfast.co.uk/apple/a1175.htm Apple a1175 Battery http://www.batteryfast.co.uk/apple/15-inch-macbook-pro.htm Apple 15 inch macbook pro Battery http://www.batteryfast.co.uk/dell/1501.htm Dell 1501 Battery http://www.batteryfast.co.uk/apple/a1185.htm Apple a1185 Battery http://www.batteryfast.co.uk/apple/a1185-white.htm Apple a1185 white Battery http://www.batteryfast.co.uk/apple/a1185.htm Apple a1185 Battery http://www.batteryfast.co.uk/apple/a1185-black.htm Apple a1185 black Battery http://www.batteryfast.co.uk/asus/a42-v6.htm Asus a42-v6 Battery http://www.batteryfast.co.uk/asus/a42-w1.htm Asus a42-w1 Battery http://www.batteryfast.co.uk/asus/w1000.htm 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.