字符串操作(ASP.NET MVC) [英] String manipulation(ASP.NET MVC)

查看:105
本文介绍了字符串操作(ASP.NET MVC)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这里有一个code,它在我的数据库中获取一条记录的一部分,并显示它并有一个链接(更多),呈现观众该记录的详细页面..

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:

我们向你保证,在U ...阅读更多

We assure you that the U... Read More

我想,最后一句话可以完成切纸之前,或切纸之前,应显示可能3句。上述示例中的最后一句话应该是'大学'。

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'.

推荐答案

你可以做一些150字符后找到的第一个空间,或者如果它不能找到一个空间延伸到了尽头。例如。

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))) & "..."%>

如果你知道有150字符后一个空间,然后:

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

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

就足够了。

这篇关于字符串操作(ASP.NET MVC)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆