# 示例代码:rindex在Python中的用法
# rindex() 方法返回子字符串 str 在字符串中最后出现的位置,如果没有找到,则抛出 ValueError 异常。
text = "hello world, hello Python"
# 查找子字符串 'hello' 最后一次出现的位置
position = text.rindex("hello")
print(f"'hello' 最后一次出现的位置是: {position}")
# 如果查找的子字符串不存在,则会抛出 ValueError
try:
position = text.rindex("Java")
except ValueError as e:
print("子字符串未找到,抛出 ValueError 异常")
rindex() 方法用于查找子字符串在字符串中最后一次出现的位置。它从字符串的末尾开始向前搜索。rindex() 返回该子字符串的索引位置;如果找不到,则会抛出 ValueError 异常。rfind() 不同的是,rfind() 在找不到子字符串时返回 -1,而 rindex() 会直接抛出异常。Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站