Javascript 在Javascript中删除字符串的第一个字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4564414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 12:58:52  来源:igfitidea点击:

Delete first character of a string in Javascript

javascriptstring

提问by Jings

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.

我想删除字符串的第一个字符,如果第一个字符是 0。0 可以多次出现。

Is there a simple function that checks the first character and deletes it if it is 0?

是否有一个简单的函数可以检查第一个字符并在它为 0 时将其删除?

Right now, I'm trying it with the JS slice()function but it is very awkward.

现在,我正在尝试使用 JSslice()功能,但它非常尴尬。

回答by Shaded

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

您可以使用以下方法删除字符串的第一个字符substr

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

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

要删除字符串开头的所有 0:

var s = "0000test";
while(s.charAt(0) === '0')
{
 s = s.substr(1);
}

回答by jave.web

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()...

非常易读的代码是将.substring()开始设置为第二个字符 (1) 的索引(第一个字符的索引为 0)。该.substring()方法的第二个参数实际上是可选的,因此您甚至不需要调用.length()...

TL;DR : Remove first character from the string:

TL;DR :从字符串中删除第一个字符:

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...

正如@Shaded 建议的那样,只要循环这个,而你的字符串的第一个字符是“不需要的”字符......

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.substr(1);
//yourString now contains "test"

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

.slice()对比.substring()对比.substr()

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

引用自(以及更多内容)String.slice 和 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.

他还指出,如果切片的参数为负,则它们从末尾引用字符串。Substring 和 substr 没有。

回答by user113716

Use .charAt()and .slice().

使用.charAt().slice()

Example:http://jsfiddle.net/kCpNQ/

示例:http : //jsfiddle.net/kCpNQ/

var myString = "0String";

if( myString.charAt( 0 ) === '0' )
    myString = myString.slice( 1 );

If there could be several 0characters at the beginning, you can change the if()to a while().

如果0开头可能有多个字符,则可以将 the 更改if()为 a while()

Example:http://jsfiddle.net/kCpNQ/1/

示例:http : //jsfiddle.net/kCpNQ/1/

var myString = "0000String";

while( myString.charAt( 0 ) === '0' )
    myString = myString.slice( 1 );

回答by Tim Down

The easiest way to strip all leading 0s is:

去除所有前导0s的最简单方法是:

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

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

如果只是剥离单个前导0字符,正如问题所暗示的那样,您可以使用

s = s.replace(/^0/, "");

回答by Andrew

You can do it with substringmethod:

您可以使用substring方法来做到这一点:

let a = "My test string";

a = a.substring(1);

console.log(a); // y test string

回答by adarshr

Did you try the substringfunction?

你试过这个substring功能吗?

string = string.indexOf(0) == '0' ? string.substring(1) : string;

Here's a reference - https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/substring

这是一个参考 - https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/substring

And you can always do this for multiple 0s:

您始终可以为多个 0 执行此操作:

while(string.indexOf(0) == '0')
{
    string = string.substring(1);
}

回答by zsalzbank

var s = "0test";
if(s.substr(0,1) == "0") {
    s = s.substr(1);
}

For all 0s: http://jsfiddle.net/An4MY/

对于所有0s:http: //jsfiddle.net/An4MY/

String.prototype.ltrim0 = function() {
 return this.replace(/^[0]+/,"");
}
var s = "0000test".ltrim0();

回答by bambolobred

//---- 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));

回答by ChaosPandion

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);

回答by bdukes

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

这是一个不假设输入是 a string,使用substring,并带有几个单元测试的:

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

    return value;
};

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

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