How do I get 3 lines of text from a paragraph

Posted by Keltex on Stack Overflow See other posts from Stack Overflow or by Keltex
Published on 2010-05-04T22:56:44Z Indexed on 2010/05/05 0:28 UTC
Read the original article Hit count: 455

Filed under:
|
|
|

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?

© Stack Overflow or respective owner

Related posts about c#

Related posts about string-manipulation