python 需要用 rindex() 来实现 lastIndexOf() 功能。
# 截取 url 最后一个 / 后的字符串:
str = "tttt/aaa/123";
print( str[ str.rindex( '/' ) + 1 : len( str ) ] ); # 打印 123
# 截取 url 倒数第二个 \ 后的字符串:
str = "qq/tttt/aaa/123";
re0 = str[ str.rindex( '/' ) + 1 : len( str ) ]
withoutLastStr = str[ 0 : str.rindex( '/' ) ]
re = withoutLastStr[ withoutLastStr.rindex( '/' ) + 1 : len( withoutLastStr ) ]
print( re + "/" + re0 ); # 打印 aaa/123
Python中使用rindex()函数
本文介绍如何在Python中利用rindex()函数实现字符串的lastIndexOf()功能,通过实例展示了如何截取URL中特定位置之后的子串,包括最后一个斜杠及倒数第二个斜杠后的字符串。
3万+

被折叠的 条评论
为什么被折叠?



