Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8351013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:30:08+00:00 2026-06-09T08:30:08+00:00

I have a string where the number of words might vary. Like: string a_string

  • 0

I have a string where the number of words might vary. Like:

string a_string = " one two three four five six seven etc etc etc "; 

How do I split the string into 5 words each, and each of those are added it to a list, such that it becomes a list of string (with each string containing 5 words). I think list would be better because number of words in string can vary, so list can grow or shrink accordingly.

I tried using Regex to get first 5 words through below line of code:

Regex.Match(rawMessage, @"(\w+\s+){5}").ToString().Trim();

but bit unsure on how to proceed further and add to list dynamically and robustly. I guess Regex can further help, or some awesome string/list function? Can you please guide me a bit?

Eventually, I would want list[0] to contain “one two three four five” and list[1] to contain “six seven etc etc etc”, and so on.. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T08:30:12+00:00Added an answer on June 9, 2026 at 8:30 am

    Splitting for words does not require regex, string provides this capability:

    var list = str.Split(' ').ToList();
    

    ToList() is a LINQ extension method for converting IEnumerable<T> objects to lists (Split() method returns an array of strings).

    To group a list by 5 words, use this code snippet:

    var res = list
        .Select((s, i) => new { Str = s, Index = i })
        .GroupBy(p => p.Index/5)
        .Select(g => string.Join(" ", g.Select(v => v.Str)));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a number of models that store words as a string with each
I have a number of strings which contain words which are bunched together and
I have a string and a number of spaces e.g. the string is accttgagattcagt
I have a string and and a number cID = 'x1' num = 1
I have a string that ends with _[a number] e.g. _1 _12 etc etc.
I have a string that contains a known number of double values. What's the
I have the string temp[256] and inside it there's a double number, such as
I have a String variable (basically an English sentence with an unspecified number of
i have a string and i need to add a number to it i.e
I have a string which needs to be split by three underscore characters. An

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.