C# Program to Split a String Collections into Groups

This is a C# Program to split string collections into groups.

Problem Description

This C# Program Splits a String Collections into Groups.

Problem Solution

Here the Given String is splited into Groups and displayed.

Program/Source Code

Here is source code of the C# Program to Split a String Collections into Groups. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Split a String Collections into Groups
 */
using System;
using System.IO;
using System.Collections;
using System.Linq;
class program
{
    static void SendEmail(string email)
    {        
        Console.WriteLine(email);
    }
    static void Main(string[] args)
    {
        string[] email = {"[email protected]", "[email protected]",
                        "[email protected]", "[email protected]",
                        "[email protected]", "[email protected]",
                        "[email protected]", "[email protected]"};
        var Grp = from i in Enumerable.Range(0, email.Length)
                       group email[i] by i / 3;
        foreach (var mail in Grp)
            SendEmail(string.Join(";", mail.ToArray()));
       Console.ReadLine();
    }
}
Program Explanation

In this C# program using email[] array variable we are reading the email Ids. The groupby is used to group the elements of a sequence according to a specified key selector function and projects the elements for each group by using a specified function.

advertisement
advertisement

The foreach statement is used to iterate through the collection to get the information that you want, but cannot be used to add or remove items from the source collection to avoid unpredictable side effects. Print the given string which is divided into groups.

Runtime Test Cases

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

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
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.