初探 String.endsWith()
前言
在之前的 初探 String.startsWith() 介紹 String.startsWith() ,該函式是檢查字首的文字,那有檢查字尾的文字嗎?有的,那就是 String.endsWith() ,在此把學習的過程做個紀錄。
內容
範例如下
let str = 'Hello world'; console.log( str.endsWith( 'world' ) );//true console.log( str.endsWith( 'world' , 10 ) );//false console.log( str.endsWith( 'world' , 100 ) );//true
String.endsWith() 用起來和 String.startsWith() ,參數直接輸入要檢查的文字即可,一樣有位移參數,意思是從該位址往前檢查文字,可以把不輸入位移參數時,這個參數就是 string.length !比較特別的是這個參數可以大於 string.length ,而且功能正常,可以範例的最後一例。
參考資料
[ developer.mozilla.org ] String.prototype.endsWith()
沒有留言:
張貼留言