How to trim a string to N chars in Javascript?

前端 未结 8 1541
暗喜
暗喜 2020-12-22 18:09

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:

var strin         


        
8条回答
  •  失恋的感觉
    2020-12-22 18:48

    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.

    var string = "this is a string";
    var length = 7;
    var trimmedString = string.substring(0, length);
    

提交回复
热议问题