C# Program to Split a String Collections into Groups
Last Updated :
26 Jan, 2022
Given a collection of strings and you are required to split them into groups using C#. The standard query operator contains GroupBy grouping operator using which we can split a collection of strings easily. The working of the GroupBy operator is similar to the SQL GroupBy clause. It is used to return the group of elements that share the common attributes or keys from the given sequence or collection.
- This supports query syntax in both the languages, C#, and VB.Net languages.
- GroupBy can also be used in the C# query. The into keyword allows you to continue with the query and can perform more query operations. Or in other words, it is a temporary identifier that allows you to perform additional query operations.
- A group with GroupBy can also be used in VB.Net.
- LINQ query is terminated with the Select or Groupby clause.
Syntax:
Grouping<TKey, TElement> object.
Example:
Input: ["Java", "Python", "Swift", "CSS", "C#", "Django"]
Output: Splitting the string collection into groups of two elements
["Java", "Python"]
["Swift", "CSS"]
["C#", "Django"]
Input: ["Delhi", "Goa", "Chennai", "Pune", "Mumbai", "Himachal", "Kolkata"]
Output: Splitting the string collection into groups of three elements
["Delhi", "Goa", "Chennai"]
["Pune", "Mumbai", "Himachal"]
["Kolkata"]
Example:
C#
// C# program to illustrate how to split a string
// collections into groups
using System;
using System.Linq;
using System.Collections.Generic;
class GFG{
static public void Main()
{
// Initializing a an array of strings
string[] subjects = { "Java", "Python", "Swift", "CSS",
"C#", "Django", "C++", "Javascript",
"HTML", "PHP" };
// Displaying the collection
Console.Write("The subjects are: \n");
Console.WriteLine(string.Join(", ", subjects));
// Now splitting the string collection into
// a group of three elements
var subjectSplit = from i in Enumerable.Range(0, subjects.Length)
group subjects[i] by i / 3;
Console.WriteLine("\nThe group of subjects are:");
foreach(var subject in subjectSplit)
subjectView(string.Join(", ", subject.ToArray()));
}
// Print each group elements
static void subjectView(string subject)
{
Console.WriteLine(subject);
}
}
Output:
The subjects are:
Java, Python, Swift, CSS, C#, Django, C++, Javascript, HTML, PHP
The group of subjects are:
Java, Python, Swift
CSS, C#, Django
C++, Javascript, HTML
PHP
Explanation: In the above C# program with the help of subjects[] array(of string type) variable we are reading different subjects. Now we split the given subject[] collection into the group of three subjects using the groupby operator. To iterate over the collection, we are using for each statement but cannot be used to add or remove items from the source collection to avoid undefined results. Now display the given string which is divided into groups.
Similar Reads
How to Split a String into an Array in C++? In C++, splitting a string into an array of substrings means we have to parse the given string based on a delimiter and store each substring in an array. In this article, we will learn how to split a string into an array of substrings in C++. Example: Input: str= âHello, I am Geek from geeksforgeeks
2 min read
How to Split a String by Multiple Delimiters in C? In C, strings are arrays of characters using string manipulation often requires splitting a string into substrings based on multiple delimiters. In this article, we will learn how to split a string by multiple delimiters in C.ExampleInput:char inputString[] = "Hello,World;This|is.GeeksForGeeks";char
2 min read
How to Split a String by a Delimiter in C? Splitting a string by a delimiter is a common task in programming. In C, strings are arrays of characters, and there are several ways to split them based on a delimiter. In this article, we will explore different methods to split a string by a delimiter in C. Splitting Strings in CThe strtok() metho
2 min read
How to Split a String by a Delimiter in C++? Splitting a string is the process of dividing the given string into multiple substrings on the basis of a character (or substring) as the separator. This separator is called delimiter and the whole process is also called tokenization.ExamplesInput: str = "geeks,for,geeks", delimiter = (,)Output: gee
4 min read
Java Program to Separate the Individual Characters from a String The string is a sequence of characters including spaces. Objects of String are immutable in java, which means that once an object is created in a string, it's content cannot be changed. In this particular problem statement, we are given to separate each individual characters from the string provided
2 min read