javascript 如何使用javascript删除前5或7个字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18403485/
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-10-27 11:46:49  来源:igfitidea点击:

how to remove first 5 or 7 characters using javascript

javascript

提问by usman610

i have posted the a question using the given javascript below:

我已经使用下面给定的 javascript 发布了一个问题:

<script type="text/javascript">
    function showData($sel){
        var str='';
        document.getElementById("demo").innerHTML;
        for (var i=0;i<sel.options.length;i++){
            str+=(str!='') ? ', '+sel.options[i].value : sel.options[i].value;
        }
    }
    sel.form.selectedFruits.value = str;
</script>

now the question is that how to remove first 5 or 7 using this javascript. please help by setting in this javascript.

现在的问题是如何使用此 javascript 删除前 5 或 7。请通过在此 javascript 中进行设置来提供帮助。

thanks in advance.

提前致谢。

回答by SpYk3HH

You could follow the substrsuggestion given by Rory, but more often than not, to remove characters in JS you use the slicemethod. For example, if you have:

您可以按照Rorysubstr给出的建议,但通常情况下,删除您使用该方法的JS 中的字符。例如,如果您有:slice

var str="Hello world!";

Removing the first 5 characters is as easy as:

删除前 5 个字符非常简单:

var n=str.slice(5);

You parameters are very simple. The first is where to start, in this case, position 5. The second is how far to go based on original string character length, in this case, nothing since we want to go to the end of the string. The result would be:

你的参数很简单。第一个是从哪里开始,在这种情况下,位置 5。第二个是根据原始字符串字符长度走多远,在这种情况下,因为我们想要走到字符串的末尾,所以什么都没有。结果将是:

" world!"

To remove the first 7 characters would be as easy as:

删除前 7 个字符就像这样简单:

var n=str.slice(7);

And the result would be:

结果将是:

"orld!"

|OR| if you wanted to GET the 5th to 7th character, you would use something like:

|或| 如果你想获得第 5 到第 7 个字符,你可以使用类似的东西:

var n=str.slice(4, 7);

The reson being that it startsat position 4 meaning the first character grabbed is the 5th character. Whereas the second parameter is what character to stop at, thus use of "7". This will stop it at the 7th character thus producing:

原因是它位置 4开始,这意味着抓取的第一个字符是第 5 个字符。而第二个参数是停止的字符,因此使用“7”。这将在第 7 个字符处停止,从而产生:

"o w"

回答by Rory McCrossan

You can use substrwith 1 parameter. This will cut the first xcharacters from the string and return the remaining.

您可以使用substr1 个参数。这将从x字符串中删除第一个字符并返回剩余的字符。

var foo = '12345String starts here';
alert(foo.substr(5)); // = "String starts here"

How you determine where to cut the string is up to you, as your question does not include enough detail.

您如何确定在何处切断字符串取决于您,因为您的问题没有包含足够的细节。

回答by Alexander

In both JavaScriptand Java:), the same syntax:

JavaScriptJava:) 中,语法相同:

"stackoverflow".substring(5); // will return "overflow"

"stackoverflow".substring(5); // will return "overflow"