string.slice(start,end) 簡中說明 EN說明
string.substr(start,length) 簡中說明 EN說明 但已經被不建議使用(還要再確認 w3schools沒有提到)
string.substr(start,length) 簡中說明 EN說明 但已經被不建議使用(還要再確認 w3schools沒有提到)
start:
開始截取的字元位置
end:
結束截取的字元位置,但不包含此字元。如果省略,表示從start取到最後一個字元
slice的start及end的值,可以使用負整數(substring不行),代表從最後一個字元(-1)往回算
字串的字元位置算法
|
||||
A
|
B
|
C
|
D
|
E
|
0
|
1
|
2
|
3
|
4
|
-5
|
-4
|
-3
|
-2
|
-1
|
<script>
var
str = "ABCDE";
var
left1 = str.substring(0,3); //ABC,相當於其他語言的left(string,3)
var
left2 = str.slice(0,3); //ABC,相當於其他語言的left(string,3)
var
right = str.slice(-3); //CDE,相當於其他語言的right(string,3)
var
mid1 = str.substring(1,4); //BCD
var
mid2 = str.slice(1,4); //BCD
var
mid3 = str.slice(-3,-1); //CD
</script>
[ 例 ] slice(start,end)
<!DOCTYPE html>
<html>
<body>
<p>The slice() method extract a part of a string
and returns the extracted parts in a new string:</br>切片()方法提取字符串的一部分,並返回所提取的部分中一個新的字符串:</p>
<p id="demo"></p>
<script>
var str = "Apple,Banana,Kiwi";
document.getElementById("demo").innerHTML = str.slice(7,13);
</script>
</body>
</html>
[ 例 ] substring(start,end)
<!DOCTYPE html>
<html>
<body>
<p>The substr() method extract a part of a string
and returns the extracted parts in a new string:</p>
<p id="demo"></p>
<script>
var str = "Apple, Banana, Kiwi";
document.getElementById("demo").innerHTML = str.substring(7,13);
</script>
</body>
</html>
[ 例 ] substr(start,length)
<!DOCTYPE html>
<html>
<body>
<p>The substr() method extract a part of a string
and returns the extracted parts in a new string:</p>
<p id="demo"></p>
<script>
var str = "Apple, Banana, Kiwi";
document.getElementById("demo").innerHTML = str.substr(7,6);
</script>
</body>
</html>
[ 例 ] Unicode 編碼:(Ans:101) 字符>>編碼數字
[ 例 ] substring(start,end)
<!DOCTYPE html>
<html>
<body>
<p>The substr() method extract a part of a string
and returns the extracted parts in a new string:</p>
<p id="demo"></p>
<script>
var str = "Apple, Banana, Kiwi";
document.getElementById("demo").innerHTML = str.substring(7,13);
</script>
</body>
</html>
[ 例 ] substr(start,length)
<!DOCTYPE html>
<html>
<body>
<p>The substr() method extract a part of a string
and returns the extracted parts in a new string:</p>
<p id="demo"></p>
<script>
var str = "Apple, Banana, Kiwi";
document.getElementById("demo").innerHTML = str.substr(7,6);
</script>
</body>
</html>
[ 例 ] Unicode 編碼:(Ans:101) 字符>>編碼數字
<script type="text/javascript"> var str="Hello world!" document.write(str.charCodeAt(1)) </script>
[ 例 ] Unicode 編碼:(Ans:HELLO,ABC) 編碼數字>>字符
<script type="text/javascript"> document.write(String.fromCharCode(72,69,76,76,79)) document.write("<br />") document.write(String.fromCharCode(65,66,67)) </script>
沒有留言:
張貼留言