定義測試使用的字元串
var text = "Hello World";
字元串
Hello World
字元對應下标
H | e | l | o | W | r | d | |||
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
字元串截取
// substring(start, end) 截取範圍:[start, end)
text.substring(6, 11)
"World"
// substr(start, length)
text.substr(6, 5)
"World"
字元串查找
// 從前向後
text.indexOf("o")
4
// 從後向前
text.lastIndexOf("o")
7
字元串和數組轉換
// 字元串轉數組
text.split(" ")
["Hello", "World"]
// 數組轉字元串
["Hello", "World"].join("-")
"Hello-World"
參考
JS截取字元串方法執行個體