Splitting Text

Regex.Split method is more powerful version of string.Split method.

In this example, we split a string, where any digit counts as a separator:


using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main(string[] args)
    {

       foreach (string s in Regex.Split ("a1b1c", @"\d")) 
          Console.Write (s + " ");
    }
}

The output:


a b c

The following splits a camel-case string into separate words:


using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main(string[] args)
    {

         foreach (string s in Regex.Split ("oneTwoThree", @"(?=[A-Z])")) 
            Console.Write (s + " ");         

    }
}

The output:


one Two Three
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.