Sunday, August 18, 2019

Displaying The First n Characters Of Text

Displaying the first n characters of a block of text on a page without chopping words off halfway through is a common requirement. 

There are a number of ways to achieve this, but here's an extension method that you might find useful.

public static class StringExtensions
{
  /// <summary>
  /// Returns part of a string up to the specified number of characters, while maintaining full words
  /// </summary>
  /// <param name="s"></param>
  /// <param name="length">Maximum characters to be returned</param>
  /// <returns>String</returns>
  public static string Chop(this string s, int length)
  {
    if (String.IsNullOrEmpty(s))
      throw new ArgumentNullException(s);
    var words = s.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
    if (words[0].Length > length)
      return words[0];
    var sb = new StringBuilder();

    foreach (var word in words)
    {
      if ((sb + word).Length > length)
        return string.Format("{0}...", sb.ToString().TrimEnd(' '));
      sb.Append(word + " ");
    }
    return string.Format("{0}...", sb.ToString().TrimEnd(' '));
  }
}
In case you didn't know, an Extension method is one that appears to endow an existing type with additional methods. In this case, the type is System.String. The String type already comes with a large number of methods, together with an array of extensions methods provided by the .NET framework developers. 
Extension methods are static methods, which are called in the same way as instance methods although you don't instantiate an instance of the object that the method acts upon. 
The type that an extension method extend is denoted by the first parameter, and it's preceded by the thiskeyword. Here are some example of how to use this extension method:
[Razor]

@myString.Chop(50)


[MVC in a Web Forms View]

<%= ViewData["myString"].ToString().Chop(50) %>


[Web Forms in an ItemTemplate]

<ItemTemplate>
   <asp:Label ID="Label1" runat="server" Text='<%# Eval("myString").ToString().Chop(50) %>' />
</ItemTemplate>

A quick explanation about the body of the method itself - as I mentioned, the first parameter is the type which is intended to be extended, and the second is the maximum number of characters to be displayed. All the words in the string that this method acts on are placed as individual items in an array by the String.Split() method. 
StringSplitOptions.RemoveEmptyEntries ensures that if there are double spaces, they will not be treated as words and added to the array as elements. The array is iterated over in a foreach loop, and elements are added to a StringBuilder object (together with a space after each element or word). 
The LINQ expression checks the current length of the StringBuilder's contents on each iteration to ensure that the maximum length specified in the parameter is not exceeded. Finally, the result has the last trailing space removed (TrimEnd()), and is returned.

No comments:

Post a Comment

No String Argument Constructor/Factory Method to Deserialize From String Value

  In this short article, we will cover in-depth the   JsonMappingException: no String-argument constructor/factory method to deserialize fro...