切掉一部分字符串 [英] cut out part of a string
本文介绍了切掉一部分字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
说,我有一个字符串
"hello is it me you're looking for"
我想删除部分字符串并返回新字符串,例如
I want to cut part of this string out and return the new string, something like
s = string.cut(0,3);
s现在等于:
"lo is it me you're looking for"
编辑:可能不是0到3.可能是5到7。
EDIT: It may not be from 0 to 3. It could be from 5 to 7.
s = string.cut(5,7);
将返回
"hellos it me you're looking for"
推荐答案
你快到了。你想要的是:
You're almost there. What you want is:
http:/ /www.w3schools.com/jsref/jsref_substr.asp
所以,在你的例子中:
Var string = "hello is it me you're looking for";
s = string.substr(3);
因为只提供一个开始(第一个arg)从该索引到字符串的结尾。
As only providing a start (the first arg) takes from that index to the end of the string.
更新,如下所示:
function cut(str, cutStart, cutEnd){
return str.substr(0,cutStart) + str.substr(cutEnd+1);
}
这篇关于切掉一部分字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文