Delete first character of a string in Javascript

前端 未结 14 1537
名媛妹妹
名媛妹妹 2020-11-28 01:01

I want to delete the first character of a string, if the first character is a 0. The 0 can be there more than once.

Is there a simple function that checks the first

14条回答
  •  孤城傲影
    2020-11-28 01:29

    String.prototype.trimStartWhile = function(predicate) {
        if (typeof predicate !== "function") {
        	return this;
        }
        let len = this.length;
        if (len === 0) {
            return this;
        }
        let s = this, i = 0;
        while (i < len && predicate(s[i])) {
        	i++;
        }
        return s.substr(i)
    }
    
    let str = "0000000000ABC",
        r = str.trimStartWhile(c => c === '0');
        
    console.log(r);

提交回复
热议问题