last-occurrence

Replace Last Occurrence of a character in a string [duplicate]

末鹿安然 提交于 2019-11-26 17:23:01
问题 This question already has an answer here: Replace last part of string 11 answers I am having a string like this "Position, fix, dial" I want to replace the last double quote(") with escape double quote(\") The result of the string is to be "Position, fix, dial\" How can I do this. I am aware of replacing the first occurrence of the string. but don't know how to replace the last occurrence of a string 回答1: String str = "\"Position, fix, dial\""; int ind = str.lastIndexOf("\""); if( ind>=0 )

How to find the last occurrence of an item in a Python list

匆匆过客 提交于 2019-11-26 11:17:49
Say I have this list: li = ["a", "b", "a", "c", "x", "d", "a", "6"] As far as help showed me, there is not a builtin function that returns the last occurrence of a string (like the reverse of index ). So basically, how can I find the last occurrence of "a" in the given list? If you are actually using just single letters like shown in your example, then str.rindex would work handily. This raises a ValueError if there is no such item, the same error class as list.index would raise. Demo: >>> li = ["a", "b", "a", "c", "x", "d", "a", "6"] >>> ''.join(li).rindex('a') 6 For the more general case you

How to find the last occurrence of an item in a Python list

我的梦境 提交于 2019-11-26 03:26:57
问题 Say I have this list: li = [\"a\", \"b\", \"a\", \"c\", \"x\", \"d\", \"a\", \"6\"] As far as help showed me, there is not a builtin function that returns the last occurrence of a string (like the reverse of index ). So basically, how can I find the last occurrence of \"a\" in the given list? 回答1: If you are actually using just single letters like shown in your example, then str.rindex would work handily. This raises a ValueError if there is no such item, the same error class as list.index