Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C# Data Types
  • C# Decision Making
  • C# Methods
  • C# Delegates
  • C# Constructors
  • C# Arrays
  • C# ArrayList
  • C# String
  • C# Tuple
  • C# Indexers
  • C# Interface
  • C# Multithreading
  • C# Exception
Open In App
Next Article:
How to Split a String into an Array in C++?
Next article icon

C# Program to Split a String Collections into Groups

Last Updated : 26 Jan, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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.


Next Article
How to Split a String into an Array in C++?
author
bhuwanesh
Improve
Article Tags :
  • C#
  • C# Programs

Similar Reads

  • C Program to Sort a String of Characters
    Sorting a string of characters refers to the process of rearranging all the characters in the given order. In this article, we will learn how to sort a string of characters using the C program. The most straightforward method to sort a string of characters is by using the qsort() function. Let's tak
    3 min read
  • C Program to Split a String into a Number of Sub-Strings
    In this article, we will learn how to split a string into a number of sub-strings using the C program. The most straightforward method to split a string into substrings using a delimiter is by using strtok() function. Let’s take a look at an example: [GFGTABS] C #include <stdio.h> #include
    3 min read
  • 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
  • C Program to Print the Length of a String using Pointers
    C language provides a built-in function strlen() but in this article, we will learn how to find and print the length of the string using pointers in C. The easiest method to find the length of a string using pointers is by calculating difference between the pointers to the first and last character o
    2 min read
  • C Program to Check if String is Pangram
    Given a string Str. The task is to check if it is Pangram or not.  A pangram is a sentence containing every letter in the English Alphabet. Examples:  Input: "The quick brown fox jumps over the lazy dog" Output: is a Pangram Explanation: Contains all the characters from ‘a’ to ‘z’]  Input: "The quic
    3 min read
  • C Program To Reverse Words In A Given String
    Example: Let the input string be "i like this program very much". The function should change the string to "much very program this like i" Examples:  Input: s = "geeks quiz practice code" Output: s = "code practice quiz geeks" Input: s = "getting good at coding needs a lot of practice" Output: s = "
    3 min read
  • C# Program To Reverse Words In A Given String
    Example: Let the input string be "i like this program very much". The function should change the string to "much very program this like i" Examples: Input: s = "geeks quiz practice code" Output: s = "code practice quiz geeks" Input: s = "getting good at coding needs a lot of practice" Output: s = "p
    4 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. Example Input:char inputString[] = "Hello,World;This|is.GeeksForGeeks";ch
    2 min read
  • JavaScript Program to Split a String into a Number of Sub-Strings
    Given a string, the task is to split the string into number of sub-strings using JavaScript. This task is common in scenarios where data needs to be segmented or processed in chunks. This article provides a flexible solution that can divide a given string into a desired number of smaller substrings,
    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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences