lastIndexOf 方法

傳回子字串最後出現在 String 物件中的位置。

function lastIndexOf(substring : String [, startindex : Number ]) : Number

引數

  • substring
    必要項。 在 String 物件內搜尋的子字串。

  • startindex
    選擇項。 整數值,指定開始在 String 物件中搜尋的索引。 若予以省略,則會從字串的結尾開始搜尋。

備註

lastIndexOf 方法會傳回一個整數值,表示子字串在 String 物件中的起始位置。 如果找不到子字串,則傳回 -1。

如果 startindex 是負數,會將 startindex 視為零處理。 如果此值大於最大的字元位置索引的話,則會將其視為最大可能索引。

搜尋是從右而左開始執行。 否則這方法就與 indexOf 相同。

範例

以下範例說明如何使用 lastIndexOf 方法。

var str = "time, time";

print ("time is at position " + str.lastIndexOf("time"));
print ("abc is at position " + str.lastIndexOf("abc"));

// Output:
//   time is at position 6
//   abc is at position -1

需求

版本 1

套用至︰

String 物件

請參閱

參考

indexOf 方法