split the string into individual strings, each containing a digit - CSharp Language Basics

CSharp examples for Language Basics:Regex

Description

split the string into individual strings, each containing a digit

Demo Code

using System;//from  w w w . ja  va 2  s. c  om
using System.Text.RegularExpressions;
class MainClass
{
    static void Main(string[] args)
    {
        string testString1 = "This sentence ends in 5 stars *****";
        Regex testRegex1 = new Regex(@"\d");
        string output = String.Empty;
        Console.Write("String split at commas [");
        // split the string into individual strings, each containing a digit
        string[] result = Regex.Split(testString1, @",\s");
        // add each digit to the output string
        foreach (var resultString in result)
            output += "\"" + resultString + "\", ";
        // delete ", " at the end of output string
        Console.WriteLine(output.Substring(0, output.Length - 2) + "]");
    }
}

Result


Related Tutorials