[英]ASP.NET MVC string formatting c# - show 20 characters of 100 - trim/cut string
I have a small trouble. 我有小麻烦。 I want to show just a part of a string for example: 我只想显示一部分字符串,例如:
Instead: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua." 取而代之的是:“ Lorem ipsum dolor amet,set diam nonumy eirmod tempor invidunt ut labour et dolore magna aliquyam erat,sed diam voluptua。”
Just a: "Lorem ipsum dolor sit amet, consetetur sadipscing..." 只是一个问题:“ Lorem ipsum dolor坐了,consetetur sadipscing ...”
Which method can I use to do that? 我可以使用哪种方法来做到这一点?
Thanks for a help and take care, Ragims 感谢您的帮助和照顾,Ragims
I use a string extension method to do this. 我使用字符串扩展方法来执行此操作。 Add a method like this to a static class wherever you keep your helper methods: 无论您在何处保留辅助方法,都将这样的方法添加到静态类中:
/// <summary>
/// Trim the front of a string and replace with "..." if it's longer than a certain length.
/// </summary>
/// <param name="value">String this extends.</param>
/// <param name="maxLength">Maximum length.</param>
/// <returns>Ellipsis shortened string.</returns>
public static string TrimFrontIfLongerThan(this string value, int maxLength)
{
if (value.Length > maxLength)
{
return "..." + value.Substring(value.Length - (maxLength - 3));
}
return value;
}
This will trim the front of the string, easy enough to fix if the beginning of your string is more important. 这将修剪字符串的开头,如果字符串的开头更重要,则很容易修复。 Then to use this in your view: 然后在您的视图中使用它:
Here is my trimmed string: <%: Model.MyValue.TrimFrontIfLongerThan(20) %>
Hope that helps! 希望有帮助!
What I always do is a "short text" and a long text. 我总是做的是“短文本”和长文本。 To avoid that words get cut off in the middle. 为避免该单词在中间被切断。 I dont know if what your exact requirements are. 我不知道您的确切要求是什么。
If it doesnt matter, use substring 如果没关系,请使用子字符串
When doing this in a grid row, I did this: @item.Body.Remove(300) and add the ellipses after this. 在网格行中执行此操作时,我执行了以下操作:@ item.Body.Remove(300)并在此之后添加省略号。 Be aware that your starting index must be greater than the value in the field. 请注意,起始索引必须大于字段中的值。 I'm using this for something where the "Body" field will be between 1000-4000 chars, so I know that 300 will always work. 我将其用于“ Body”字段在1000-4000个字符之间的事物,因此我知道300总是可以工作的。 See below: 见下文:
@foreach (var item in Model) {
<tr>
<td>@Html.ActionLink(item.Headline, "Edit", new { id=item.AdvisoryId })</td>
<td>@Html.Raw(item.Body.Remove(300))...</td>
<td>@item.AdvisoryStartDate.ToShortDateString()</td>
<td>@item.AdvisoryType.AdvisoryType</td>
<td>@item.AdvisoryProvider.AdvisoryProvider</td>
<td>@item.AdvisoryCategory.AdvisoryCategory</td>
<td>@Html.ActionLink("View", "Details", new { id=item.AdvisoryId })</td>
</tr>
}
MODEL - helps make sure there's no bug 模型-帮助确保没有错误
[MinLength(300, ErrorMessage = "Body must be longer than 300 characters.")]
[MaxLength(4000, ErrorMessage = "Body cannot be longer than 4000 characters.")]
public string Body { get; set; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.