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 778443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:45:53+00:00 2026-05-14T19:45:53+00:00

I’m trying to create an snippet from a paragraph. I have a long paragraph

  • 0

I’m trying to create an “snippet” from a paragraph. I have a long paragraph of text with a word hilighted in the middle. I want to get the line containing the word before that line and the line after that line.

I have the following piece of information:

  • The text (in a string)
  • The lines are deliminated by a NEWLINE character \n
  • I have the index into the string of the text I want to hilight

A couple other criteria:

  • If my word falls on first line of the paragraph, it should show the 1st 3 lines
  • If my word falls on the last line of the paragraph, it should show the last 3 lines
  • Should show the entire paragraph in the degenative cases (the paragraph only has 1 or 2 lines)

Here’s an example:

This is the 1st line of CAT text in the paragraph
This is the 2nd line of BIRD text in the paragraph
This is the 3rd line of MOUSE text in the paragraph
This is the 4th line of DOG text in the paragraph
This is the 5th line of RABBIT text in the paragraph

Example, if my index points to BIRD, it should show lines 1, 2, & 3 as one complete string like this:

This is the 1st line of CAT text in the paragraph
This is the 2nd line of BIRD text in the paragraph
This is the 3rd line of MOUSE text in the paragraph

If my index points to DOG, it should show lines 3, 4, & 5 as one complete string like this:

This is the 3rd line of MOUSE text in the paragraph
This is the 4th line of DOG text in the paragraph
This is the 5th line of RABBIT text in the paragraph

etc.

Anybody want to help tackle this?

  • 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-05-14T19:45:54+00:00Added an answer on May 14, 2026 at 7:45 pm

    Using the LINQ extension methods to get the right strings:

    string[] lines = text.Split('\n');
    
    // Find the right line to work with
    int position = 0;
    for (int i = 0; i < lines.Count(); i++)
      if (lines[i].Contains(args[0]))
        position = i - 1;
    
    // Get in range if we had a match in the first line
    if (position == -1)
      position = 0;
    
    // Adjust the line index so we have 3 lines to work with
    if (position > lines.Count() - 3)
      position = lines.Count() - 3;
    
    string result = String.Join("\n", lines.Skip(position).Take(3).ToArray());
    

    This can of course be optimized a bit by quitting the for loop as soon as the index has been found, and probably a number of other things. You can probably even LINQify so you never need to actually store that extra array, but I can’t think of a good way to do that right now.

    An alternative for the checks on position could be something like position = Math.Max(0,Math.Min(position, lines.Count() - 3)); – which would handle both of them at once.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.