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

本文翻译自:Delete first character of a string in Javascript

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. 现在,我正在尝试使用JS slice()函数,但它非常尴尬。


#1楼

参考:https://stackoom.com/question/J9Pa/在Javascript中删除字符串的第一个字符


#2楼

From the Javascript implementation of trim() > that removes and leading or ending spaces from strings. 从trim()>的Javascript实现中删除和引导或结束字符串中的空格。 Here is an altered implementation of the answer for this question. 以下是对此问题的答案的更改实现。

var str = "0000one two three0000"; //TEST  
str = str.replace(/^\s+|\s+$/g,'0'); //ANSWER

Original implementation for this on JS 关于JS的原始实现

string.trim():
if (!String.prototype.trim) {
 String.prototype.trim = function() {
  return this.replace(/^\s+|\s+$/g,'');
 }
}

#3楼

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

#4楼

Very readable code is to use .substring() with a start set to index of the second character (1) (first character has index 0). 非常易读的代码是使用.substring() ,其开头设置为第二个字符的索引(1)(第一个字符的索引为0)。 Second parameter of the .substring() method is actually optional, so you don't even need to call .length() ... .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() vs .substring() vs .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. 子串和子串不。


#5楼

Did you try the substring function? 你尝试过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);
}

#6楼

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

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

String.prototype.ltrim0 = function() {
 return this.replace(/^[0]+/,"");
}
var s = "0000test".ltrim0();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值