字串的 lastIndexOf() 方法

了解 JavaScript 字串的 lastIndexOf() 方法的一切 lastIndexOf() 方法會回傳參數所傳入的字串在目前字串中的最後出現位置。 如果搜索字串沒有被找到,則返回 -1。 'JavaScript is a great language. Yes I mean JavaScript'.lastIndexOf('Script') //47 'JavaScript'.lastIndexOf('C++') //-1 同時請參考indexOf()。

字串的 slice() 方法

學習 JavaScript 中的字串 slice() 方法 從字串中的特定位置 begin 到 end 之間的部分回傳一個新的字串。 原始字串不會被改變。 end 是可選的。 'This is my car'.slice(5) // is my car 'This is my car'.slice(5, 10) // is my 如果第一個參數設定為負數,起始索引將從結尾開始,第二個參數必須也是負數,始終從結尾計算: 'This is my car'.slice(-6) // my car 'This is my car'.slice(-6, -4) // my

字串的 startsWith() 方法

深入了解 JavaScript 的字串 startsWith() 方法 檢查一個字串是否以傳入的字串值開頭。 你可以在任何字串上呼叫 startsWith(),傳入一個子字串,並檢查結果是否為 true 或 false: 'testing'.startsWith('test') //true 'going on testing'.startsWith('test') //false 這個方法接受第二個參數,讓你指定想要從哪個字元開始檢查: 'testing'.startsWith('test', 2) //false 'going on testing'.startsWith('test', 9) //true