views:

310

answers:

4

i have here a code that gets a portion of a record on my database and display it and has a link ("Read More") that renders the viewer to the detailed page of that record..

    <%  Dim id As Integer = _news.Rows(count).Item("IDnews")%>
    <%=_news.Rows(count).Item("newsTitle")%>
    <img src='<%= Url.Content("~/NewsPictures/" + _news.Rows(count).Item("newsThumbnail")) %>' alt="" />
    <%Dim content As String = _news.Rows(count).Item("newsContent")%>

    <%If content.Length > 50 Then%>
    <%content = content.Substring(0, 150) & "..."%> 
    <%End If%>

    <%=content%>
    <%=Html.ActionLink("Read More", "NewsPublic", "Administration", New With {id}, DBNull.Value)%>

it displays something like : We assure you that the U... Read More

i would like that the last word be completed before it is cut. or maybe 3 sentences should be displayed before it is cut.. the last word in the above sample should be 'University'...

thank you! God bless!

A: 
content.Substring(0, content.IndexOf(" ", 150))
Jan Jongboom
What happens if there is no space in the first 150 characters? no content will be displayed.
Kaius
+2  A: 

you could do something which finds the first space after the 150th character, or if it cant find a space extends to the end. e.g.

<%content = content.Substring(0, (content.IndexOf(" ", 150) < 0 ? content.Length : content.IndexOf(" ", 150))) & "..."%>

If you know there is a space after the 150 character then:

<%content = content.Substring(0, content.IndexOf(" ", 150)) & "..."%>

would be sufficient

JDunkerley
Does this not result in more spaghetti code than he already has?
Kaius
im getting an error on this "Overload resolution failed because no accessible 'Substring' accepts this number of arguments"
madphp
@madphp: Have corrected, there was a , instead of the ?
JDunkerley
A: 

Replace the line

<%content = content.Substring(0, 150) & "..."%>

with

<%content = GetStartOfString(content, 150) %>

Then create a function similar to this in a utilities class or wherever you keep code that you reuse.

public static string GetStartOfString(string s, int length)
{
        if (s.Length <= length)
        {
            return s;
        }

        if(s.IndexOf(" ",length) > 0)
            return s.Substring(0, s.IndexOf(" ",length));   

        return s.substring(0,length);
 }

This way you have all the code in one place rather than spread across multiple places. (DRY) Also you could globalize the length in this method and have it work site wide with just a small change.

Kaius
returns a bool?
David Liddle
fair point, corrected that now.
Kaius
A: 

An alternative solution would be to have 2 fields in your database. One for the main Content and one for a Headline. The Headline provides a summary of the main Content and be limited to 150 characters. This would avoid any spaghetti code in your View and your content would be better described.

David Liddle
I wouldnt recommend changing the Database schema just to display a shortened version of the text. If the same text is used extensively througout the site then perhaps but im not sure this is the case here.
Kaius
I think many news and article websites adopt this approach - http://news.bbc.co.uk and http://www.nytimes.com. It would seem pointless to just cut off part of a sentence. Say the end of the cut off just gave you two words, why would you want those extra two words? It takes up more space and is useless information to a reader.
David Liddle