Delete first character of a string in Javascript

前端 未结 14 1598
名媛妹妹
名媛妹妹 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:32

    Here's one that doesn't assume the input is a string, uses substring, and comes with a couple of unit tests:

    var cutOutZero = function(value) {
        if (value.length && value.length > 0 && value[0] === '0') {
            return value.substring(1);
        }
    
        return value;
    };
    

    http://jsfiddle.net/TRU66/1/

提交回复
热议问题