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()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。