Delete first character of a string in Javascript

前端 未结 14 1458
名媛妹妹
名媛妹妹 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
    //---- remove first and last char of str    
    str = str.substring(1,((keyw.length)-1));
    
    //---- remove only first char    
    str = str.substring(1,(keyw.length));
    
    //---- remove only last char    
    str = str.substring(0,(keyw.length));
    
    0 讨论(0)
  • 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);

    0 讨论(0)
  • 2020-11-28 01:30

    The easiest way to strip all leading 0s is:

    var s = "00test";
    s = s.replace(/^0+/, "");
    

    If just stripping a single leading 0 character, as the question implies, you could use

    s = s.replace(/^0/, "");
    
    0 讨论(0)
  • 2020-11-28 01:31

    Very readable code is to use .substring() with a start set to index of the second character (1) (first character has index 0). Second parameter of the .substring() method is actually optional, so you don't even need to call .length()...

    TL;DR : Remove first character from the string:

    str = str.substring(1);
    

    ...yes it is that simple...

    Removing some particular character(s):

    As @Shaded suggested, just loop this while first character of your string is the "unwanted" character...

    var yourString = "0000test";
    var unwantedCharacter = "0";
    //there is really no need for === check, since we use String's charAt()
    while( yourString.charAt(0) == unwantedCharacter ) yourString = yourString.substring(1);
    //yourString now contains "test"
    

    .slice() vs .substring() vs .substr()

    EDIT: substr() is not standardized and should not be used for new JS codes, you may be inclined to use it because of the naming similarity with other languages, e.g. PHP, but even in PHP you should probably use mb_substr() to be safe in modern world :)

    Quote from (and more on that in) What is the difference between String.slice and String.substring?

    He also points out that if the parameters to slice are negative, they reference the string from the end. Substring and substr doesn´t.

    0 讨论(0)
  • 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/

    0 讨论(0)
  • 2020-11-28 01:34

    You can remove the first character of a string using substring:

    var s1 = "foobar";
    var s2 = s1.substring(1);
    alert(s2); // shows "oobar"
    

    To remove all 0's at the start of the string:

    var s = "0000test";
    while(s.charAt(0) === '0')
    {
     s = s.substring(1);
    }
    
    0 讨论(0)
提交回复
热议问题