我有这个URL
https://i.nhentai.net/galleries/1545079/1.jpg
我需要删除最后一个“ /”之后的所有内容,最好将字符串“ /”保留在字符串中,仅删除“ 1.jpg”。我在网上找不到任何答案,这是我的最后选择。
答案 0 :(得分:1)
URL = https://i.nhentai.net/galleries/1545079/1.jpg
# considering https:// as every start
URL = URL[8:]
# splits and excludes last item
new_url = URL.split('/')[:-1]
# re-joins the url
new_url = '/'.join(new_url)
# adds constant start
new_url = 'https://' + new_url
答案 1 :(得分:0)
从最后一个索引到第一个索引运行一个循环,并在第一次出现'/'时停止。然后根据该索引建立一个新字符串。
x = "https://i.nhentai.net/galleries/1545079/1.jpg"
newStr = ""
for i in range(len(x) - 1, 0, -1):
if x[i] == '/':
newStr = x[0:i + 1]
break
答案 2 :(得分:0)
如果仅用于此特定URL,则可以切片:
>>> print('https://i.nhentai.net/galleries/1545079/1.jpg'[:-5])
https://i.nhentai.net/galleries/1545079/