MVC Razor need to get Substring

前端 未结 8 603
栀梦
栀梦 2020-12-18 23:24

I have the following inside of my view

     @Html.DisplayFor(modelItem => item.FirstName)

I need to get the first initial of the First N

8条回答
  •  再見小時候
    2020-12-19 00:17

    You can use a custom extension method as shown below:

    /// 
    /// Returns only the first n characters of a String.
    /// 
    /// 
    /// 
    /// 
    /// 
    public static string TruncateString(this string str, int start, int maxLength)
    {        
        return str.Substring(start, Math.Min(str.Length, maxLength));
    }
    

    Hope this helps...

提交回复
热议问题