我在表中有一个字符串:http://www.foo.com/hello/bar/baz
我想获取最后一个“/”之后的所有内容(在此大小写字符串 'baz')。
我可以做 substr(str, lastIndexOf(str, '/')),但我不知道如何在 SQLite 中获取 lastIndexOf。
最佳答案
select replace(str, rtrim(str, replace(str, '/', '')), '') from table;
逐步解释。例如,我们有字符串:
/storage/udisk/1200 Mics/[2002] 1200 微克/1200 Mics - 03 - Mescaline.mp3
replace(str, '/', '')
从 str 中删除 /
字符,因此我们将拥有:
storageudisk1200 Mics[2002] 1200 Micrograms1200 Mics - 06 - Ecstasy.mp3
我们称它为 noslashes
。接下来我们使用 rtrim
(str, noslashes)
,这将删除出现在 noslashes
中的所有字符,从右边开始。因为 noslashes
包含字符串中除 /
之外的所有内容,所以这将从右边开始修剪,直到找到 /
字符。这样我们找到了我们的父目录:
/storage/udisk/1200 Mics/[2002] 1200 微克/
现在我们使用 replace
从文件路径中删除父路径名,我们只有文件名
1200 麦克风 - 03 - Mescaline.mp3
关于sqlite - 如何在 SQLite 中获取子字符串的最后一个索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21388820/