ASP.NET MVC子的帮助 [英] ASP.NET MVC SubString help
本文介绍了ASP.NET MVC子的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个ASP.NET MVC应用程序,显示新闻文章和主要段落我有一个截断和HTML标记脱衣舞。例如< P><%= item.story.RemoveHTMLTags()截断()%方式>< / P>
I have an ASP.NET MVC app that shows news articles and for the main paragraph I have a truncate and HTML tag stripper. e.g. <p><%= item.story.RemoveHTMLTags().Truncate() %></p>
的两个函数是从一个延伸和如下所示:
The two functions are from an extension and are as follows:
public static string RemoveHTMLTags(this string text)
{
return Regex.Replace(text, @"<(.|\n)*?>", string.Empty);
}
public static string Truncate(this string text)
{
return text.Substring(0, 200) + "...";
}
然而,当我创建一个新的物品与3-4只字的故事说出来会抛出这个错误:索引和长度必须引用在字符串中的位置。
参数名称:长度
这是什么问题?谢谢
推荐答案
更改您的截断功能是:
public static string Truncate(this string text)
{
if(text.Length > 200)
{
return text.Substring(0, 200) + "...";
}
else
{
return text;
}
}
一个有用得多的版本是
public static string Truncate(this string text, int length)
{
if(text.Length > length)
{
return text.Substring(0, length) + "...";
}
else
{
return text;
}
}
这篇关于ASP.NET MVC子的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文