string.slice()是javascript中的内置函数,用于返回给定输入字符串的一部分或片段。
用法:
string.slice(startingindex, endingindex)
参数:此函数使用两个参数Beginningindex(应从其开始索引,字符串)和Endingindex(应在其中包含索引,字符串之前)。
返回值:它返回给定输入字符串的一部分或一部分。
代码1:
<script>
// Taking a string as input.
var A = 'Ram is going to school';
// Calling of slice() function.
b = A.slice(0, 5);
// Here starting index is 1 given
// and ending index is not given to it so
// it takes to the end of the string
c = A.slice(1);
// Here endingindex is -1 i.e, second last character
// of the given string.
d = A.slice(3, -1);
e = A.slice(6);
document.write(b +"<br>");
document.write(c +"<br>");
document.write(d +"<br>");
document.write(e);
</script>
输出:
Ram i am is going to school is going to schoo going to school
注:本文由纯净天空筛选整理自ShivamKD大神的英文原创作品 JavaScript | string.slice()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。