Truncate a string straight JavaScript

Use the substring method: var length = 3; var myString = “ABCDEFG”; var myTruncatedString = myString.substring(0,length); // The value of myTruncatedString is “ABC” So in your case: var length = 3; // set to the number of characters you want to keep var pathname = document.referrer; var trimmedPathname = pathname.substring(0, Math.min(length,pathname.length)); document.getElementById(“foo”).innerHTML = “<a href=”” … Read more

Limiting double to 3 decimal places

Doubles don’t have decimal places – they’re not based on decimal digits to start with. You could get “the closest double to the current value when truncated to three decimal digits”, but it still wouldn’t be exactly the same. You’d be better off using decimal. Having said that, if it’s only the way that rounding … Read more

How do I truncate a .NET string?

There isn’t a Truncate() method on string, unfortunately. You have to write this kind of logic yourself. What you can do, however, is wrap this in an extension method so you don’t have to duplicate it everywhere: public static class StringExt { public static string Truncate(this string value, int maxLength) { if (string.IsNullOrEmpty(value)) return value; … Read more