查找字符串中子字符串的最后一次出现,将其替换 [英] Finding last occurrence of substring in string, replacing that

查看:378
本文介绍了查找字符串中子字符串的最后一次出现,将其替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一长串相同格式的字符串,我想找到最后一个".字符中的每个字符,然后将其替换为.-".我已经尝试过使用rfind,但似乎无法正确地利用它来执行此操作.

So I have a long list of strings in the same format, and I want to find the last "." character in each one, and replace it with ". - ". I've tried using rfind, but I can't seem to utilize it properly to do this.

推荐答案

这应该做到

old_string = "this is going to have a full stop. some written sstuff!"
k = old_string.rfind(".")
new_string = old_string[:k] + ". - " + old_string[k+1:]

这篇关于查找字符串中子字符串的最后一次出现,将其替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆