---
# System prepended metadata

title: Replace duplicate substring in a string
tags: [Strings]

---


Replace duplicate substring in a string
===


`aaa.heic.jjj.heic` to `aaa.heic.jjj.jpg`
```swift=
let str = "aaa.heic.jjj.heic"

if let range = str.lowercased().range(of:".heic", options: [.backwards], range: nil, locale: nil) {
    str.replacingCharacters(in: range, with: ".jpg")
}
```


## Ref.
[How do I find the last occurrence of a substring in a Swift string?](https://stackoverflow.com/a/44713353)

###### tags: `Strings`