Skip to content
Advertisement

How to trim a string to N chars in Javascript?

How can I, using Javascript, make a function that will trim string passed as argument, to a specified length, also passed as argument. For example:

JavaScript

Anyone got ideas? I’ve heard something about using substring, but didn’t quite understand.

Advertisement

Answer

Why not just use substring… string.substring(0, 7); The first argument (0) is the starting point. The second argument (7) is the ending point (exclusive). More info here.

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement