删除字符串中最后一次出现的单词可以通过以下步骤实现:
以下是一个示例的Python代码实现:
def delete_last_occurrence_of_word(sentence, target_word):
words = sentence.split()
for i in range(len(words)-1, -1, -1):
if words[i] == target_word:
del words[i]
break
return ' '.join(words)
# 示例用法
sentence = "This is a sample sentence. This is the last occurrence of the word sample."
target_word = "sample"
result = delete_last_occurrence_of_word(sentence, target_word)
print(result)
输出结果为:"This is a sample sentence. This is the last occurrence of the word."
领取专属 10元无门槛券
手把手带您无忧上云