C# Program to Implement String Splitter

This is a C# Program to implement string splitter.

Problem Description

This C# Program Implements String Splitter.

Problem Solution

Here it Returns a string array that contains the substrings in this string that are delimited by elements of a specified string array.

Program/Source Code

Here is source code of the C# Program to Implement String Splitter. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Implement String Splitter
 */
using System;
using System.Text.RegularExpressions;
class Program
{
    static void Main()
    {
        string sentence = "School had 40 Rooms, 500 Boys, 500 Girls and 250 Teachers";
        string[] digits = Regex.Split(sentence, @"\D+");
        foreach (string value in digits)
        {
            int number;
            if (int.TryParse(value, out number))
            {
                Console.Write(value);
            }
            Console.ReadLine();
        }
    }
}
Program Explanation

This C# program is used to implement string splitter. We have already defined a sentence using ‘sentence’ variable. The Regex.Split() function is used to split an input string into an array of substrings at the position, defined by a regular expression pattern specified in the Regex constructor. Return a string array that contains the substrings in this string that are delimited by elements of a specified string array.

advertisement
advertisement
Runtime Test Cases
 
40
500
500
250

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.