locked
Retreiving A paragraph in a memo field (Access / Repeater) RRS feed

  • Question

  • User1360724232 posted

    Helo,

    I have a page, with a repeater which pulls out news stories from an access data source. It has an ID, Title, Article Content and date published. What ive done is paste the entire article into one memo field. It works well when displaying a single story, but if i want to display a list of different stories, how would i say, display the title, date and ONLY the first paragraph of the article field?

    I've played around with substring and say, display the first 60 characters, but it tends to be unreliable and words and sentences are left unfinished.

     Html is inside the article field, so i guess the best way would be to stop after the first <p /> tag or something.

    Without some special sql code, the only other way i can think of is to create another field called summary, and copy the first paragraph into that field.

    Monday, March 9, 2009 5:57 AM

Answers

  • User-1757793930 posted

    Yes you can do something like that or you can use substring like you were talking about but add a little complexity to your substring.

    Lets say 60 characters so you do something like below

    C#

    string summary = Result["story"].ToString().Substring(0,70)
     
    Now notice i did 70 characters from here we are going to check the last index of space. So i also specified that the string must be greater than 50 characters and the summary last index of " " is > 0
     
      if (summary.LastIndexOf(" ") > 0 && summart.Substring(0, summart.LastIndexOf(" ")).ToString().Length > 50 )
            {
                summary = summary.Substring(0, summary.LastIndexOf(" "));
            }
     
     
     This produces what we would like, simply makes sure that the last word is not cut off. The next thing you might want to do is through some regular expressions in there to remove the HTML for showing summaries, as cutting of partial HTML will cause your page markup to be incomplete
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 9, 2009 6:31 AM
  • User-1199946673 posted

    Without some special sql code, the only other way i can think of is to create another field called summary, and copy the first paragraph into that field.

    Or you can write the first paragraph in the summary field, and the rest of the article in the other field, so you won't have duplicate data!!!

    Recently there was a similiar question:

    http://forums.asp.net/p/1364376/2829436.aspx#2829436

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 9, 2009 6:37 AM

All replies

  • User-1757793930 posted

    Yes you can do something like that or you can use substring like you were talking about but add a little complexity to your substring.

    Lets say 60 characters so you do something like below

    C#

    string summary = Result["story"].ToString().Substring(0,70)
     
    Now notice i did 70 characters from here we are going to check the last index of space. So i also specified that the string must be greater than 50 characters and the summary last index of " " is > 0
     
      if (summary.LastIndexOf(" ") > 0 && summart.Substring(0, summart.LastIndexOf(" ")).ToString().Length > 50 )
            {
                summary = summary.Substring(0, summary.LastIndexOf(" "));
            }
     
     
     This produces what we would like, simply makes sure that the last word is not cut off. The next thing you might want to do is through some regular expressions in there to remove the HTML for showing summaries, as cutting of partial HTML will cause your page markup to be incomplete
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 9, 2009 6:31 AM
  • User-1199946673 posted

    Without some special sql code, the only other way i can think of is to create another field called summary, and copy the first paragraph into that field.

    Or you can write the first paragraph in the summary field, and the rest of the article in the other field, so you won't have duplicate data!!!

    Recently there was a similiar question:

    http://forums.asp.net/p/1364376/2829436.aspx#2829436

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 9, 2009 6:37 AM
  • User1360724232 posted

    thanks! do you have vb code for that?

    Monday, March 9, 2009 8:33 AM
  • User-360979722 posted

    I've adapted this to VB

    Const MaxStringLength As Integer = 250
    Function Summarise(ByVal value As String) As String
    Dim summary As String = value
    summary = Regex.Replace(summary, "<(.|\n)*?>", String.Empty)
    If summary.Length > MaxStringLength Then
    summary = summary.ToString().Substring(0, MaxStringLength)
    If (summary.LastIndexOf(" ") > 0 And summary.Substring(0, summary.LastIndexOf(" ")).ToString().Length > 50) Then
    summary = summary.Substring(0, summary.LastIndexOf(" "))
    End If
    summary &= ".."
    End If
    Return summary

    End Function



    Thursday, August 27, 2009 1:01 PM