當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Dart String.lastIndexOf用法及代碼示例

dart:core 庫中String.lastIndexOf 方法的用法介紹如下。

用法:

int lastIndexOf(
   Pattern pattern,    
   [int? start]   
)

此字符串中最後一個匹配pattern 的起始位置。

通過從 start 開始向後搜索來查找模式匹配:

const string = 'Dartisans';
print(string.lastIndexOf('a')); // 6
print(string.lastIndexOf(RegExp(r'a(r|n)'))); // 6

如果在此字符串中找不到 pattern,則返回 -1。

const string = 'Dartisans';
print(string.lastIndexOf(RegExp(r'DART'))); // -1

如果省略start,則從字符串末尾開始搜索。如果提供, start 必須為非負數且不大於 length

相關用法


注:本文由純淨天空篩選整理自dart.dev大神的英文原創作品 lastIndexOf method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。