Answered by:
Get paragraph between two paragraph in C#

Question
-
User29410129 posted
how to get specific paragraph from more than two paragraph in C#. whole line are stored in var
Paragraph 1 start
// multiple line in paragraph 1
Paragraph 1 End
Paragraph 2 start
// multiple line in paragraph 2
Paragraph 2 start
Paragraph 3 start
// multiple line in paragraph 3
Paragraph 3 end
I want to get text between "Paragraph 3 start" and "Paragraph 3 end" from the variable. how i can implement this scenario?
Thursday, July 13, 2017 7:11 AM
Answers
-
User475983607 posted
Finding text in string requires a delimiter of some sort. Paragraphs are separated by two new line character. Use this fact to split the paragraphs into an array then select the second index, paragraph 3.
namespace ConsoleDemo { class Program { static void Main(string[] args) { string text = @"paragraph 1 some text paragraph 2 paragraph 3 with text and a new line"; string[] paragraphs = text.Split(new string[] { Environment.NewLine + Environment.NewLine }, StringSplitOptions.None); Console.WriteLine(paragraphs[2]); } } }
Resultsparagraph 3 with text and a new line
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, July 13, 2017 10:33 AM -
User-707554951 posted
Hi DanyalHaider,
Working code based on your needs:
var MultiParagraphString = @"Paragraph 1 start line in paragraph 1 line in paragraph 1 Paragraph 1 End Paragraph 2 start line in paragraph 2 line in paragraph 2 line in paragraph 2 Paragraph 2 start Paragraph 3 start line in paragraph 3 line in paragraph 3 line in paragraph 3 line in paragraph 3 Paragraph 3 end"; int par = 3;//get text between "Paragraph 3 start" and "Paragraph 3 end"; string[] paragraphs = MultiParagraphString.Split(new string[] { "\r\n" }, StringSplitOptions.None); List<int> indes = new List<int>(); for (int i = 0; i < paragraphs.Length; i++) { //paragraphs[i].Contains("3") if (paragraphs[i].Contains("Paragraph 3 start")) { indes.Add(i); } if (paragraphs[i].Contains("Paragraph 3 end")) { indes.Add(i); } } for (int j = indes[0]+1; j < indes[1]; j++) { Response.Write(paragraphs[j]); }
Best regards
Cathy
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, July 14, 2017 2:55 AM
All replies
-
User1970566204 posted
hi,
may i know your are using (asp.net + c# [web page]) or (c# [win form] ) !?
Thursday, July 13, 2017 9:04 AM -
User29410129 posted
I am using Asp.net + C#
Thursday, July 13, 2017 9:40 AM -
User1970566204 posted
working samples;
aspx page
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="paragraph.aspx.cs" Inherits="test.paragraph" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Literal runat="server" ID="ltrPageContent"></asp:Literal> </div> <div> Enter the Paragraph number : <asp:TextBox ID="txtParagraphNumber" runat="server"></asp:TextBox> </div> <div> <asp:Button ID="btnGet" runat="server" Text="Get Paragraph" OnClick="btnGet_Click" /></div> requested paragraph ::<asp:Literal ID="ltrRequested" runat="server"></asp:Literal> </form> </body> </html>
code file
using System; namespace test { public partial class paragraph : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //get Page Content var iPageContent = getPageContent(); //show Page Content ltrPageContent.Text = iPageContent; //get paragraph ltrRequested.Text = getParagraph(iPageContent, 4); } private string getPageContent() { //html tag <p> return "<p>paragraph 1..................</p><p>paragraph 2...........</p><p>paragraph 3..........</p>"; } private string getParagraph(string iPageContent ,int Index) { string[] Spliter = new string[] { "<p>" }; string[] paragraphs = iPageContent.Split(Spliter, StringSplitOptions.RemoveEmptyEntries); return paragraphs.Length >= Index ? paragraphs[Index - 1].ToString().Replace("</p>","") : "specified paragraph not found"; } //based on give paragraph number protected void btnGet_Click(object sender, EventArgs e) { ltrRequested.Text= getParagraph(getPageContent(), Convert.ToInt32(txtParagraphNumber.Text.Trim())); } } }
Thursday, July 13, 2017 9:51 AM -
User475983607 posted
Finding text in string requires a delimiter of some sort. Paragraphs are separated by two new line character. Use this fact to split the paragraphs into an array then select the second index, paragraph 3.
namespace ConsoleDemo { class Program { static void Main(string[] args) { string text = @"paragraph 1 some text paragraph 2 paragraph 3 with text and a new line"; string[] paragraphs = text.Split(new string[] { Environment.NewLine + Environment.NewLine }, StringSplitOptions.None); Console.WriteLine(paragraphs[2]); } } }
Resultsparagraph 3 with text and a new line
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, July 13, 2017 10:33 AM -
User29410129 posted
No no, This is not what i am looking:
Var text = "
Paragraph 1 start
// multiple line in paragraph 1
Paragraph 1 End
Paragraph 2 start
// multiple line in paragraph 2
Paragraph 2 start
Paragraph 3 start
// multiple line in paragraph 3
Paragraph 3 end"
i want to do operation on this variable to get required paragraph using C#
Thursday, July 13, 2017 11:03 AM -
User303363814 posted
Are you saying that a paragraph literally starts with the words
Paragraph n start
or is there some other way to know when a paragraph starts? Is this an html document where paragraphs are between <p> and </p> elements? Is this a word document? .doc or .docx? Is this some other format?
Friday, July 14, 2017 1:12 AM -
User-707554951 posted
Hi DanyalHaider,
Working code based on your needs:
var MultiParagraphString = @"Paragraph 1 start line in paragraph 1 line in paragraph 1 Paragraph 1 End Paragraph 2 start line in paragraph 2 line in paragraph 2 line in paragraph 2 Paragraph 2 start Paragraph 3 start line in paragraph 3 line in paragraph 3 line in paragraph 3 line in paragraph 3 Paragraph 3 end"; int par = 3;//get text between "Paragraph 3 start" and "Paragraph 3 end"; string[] paragraphs = MultiParagraphString.Split(new string[] { "\r\n" }, StringSplitOptions.None); List<int> indes = new List<int>(); for (int i = 0; i < paragraphs.Length; i++) { //paragraphs[i].Contains("3") if (paragraphs[i].Contains("Paragraph 3 start")) { indes.Add(i); } if (paragraphs[i].Contains("Paragraph 3 end")) { indes.Add(i); } } for (int j = indes[0]+1; j < indes[1]; j++) { Response.Write(paragraphs[j]); }
Best regards
Cathy
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, July 14, 2017 2:55 AM -
User-1838255255 posted
Hi DanyalHaider,
According to your description, as far as I know, you want to get the some string from a long string? You say have three paragraphs, but I am not clear you how to define this paragraph. I think you need split string through something like space, comma etc.
I think you could post the content for us to split!
Best Regards,
Eric Du
Friday, July 14, 2017 10:17 AM