How to Remove last Comma?

后端 未结 12 1941
自闭症患者
自闭症患者 2020-12-08 06:58

This code generates a comma separated string to provide a list of ids to the query string of another page, but there is an extra comma at the end of the string. How can I re

12条回答
  •  感动是毒
    2020-12-08 07:54

    you can use below extension method:

    String.prototype.trimEnd = function (c) {
        c = c ? c : ' ';
        var i = this.length - 1;
        for (; i >= 0 && this.charAt(i) == c; i--);
        return this.substring(0, i + 1);
    }
    

    So that you can use it like :

    var str="hello,";
    str.trimEnd(',');
    

    Output: hello.

    for more extension methods, check below link: Javascript helper methods

提交回复
热议问题