Wednesday, January 18, 2017

Javascipt - Cut Html Text with nice ending.


The item.Teaser = "Something and then something. or somewhat like";
Will be cut to down with a ... ending.

Javascript
  let teaser = item.Teaser.substring(0,35);  
    let teasercut = teaser.substr(0, Math.min(teaser.length, teaser.lastIndexOf(" "))) + '...';  

C#
string shortDescription = DescriptionString.Substring(0, 255);
   var shortDescriptionfixed = shortDescription?.Substring(0, Math.Min(shortDescription.Length, shortDescription.LastIndexOf(" ", StringComparison.Ordinal)));
   shortDescriptionfixed +="...";

No comments:

Post a Comment