Garden 🌻

String::replace

Replace occurrences of before with after in this string.

"abc xabc".replace("abc", "def") //-> "def xdef"
Source Codepublic method replace(this: String, before: String, after: String): String {
  let parts: List<String> = []

  let s = this
  while True {
    match s.index_of(before) {
      Some(i) => {
        parts = parts.append(s.substring(0, i))
        parts = parts.append(after)
        s = s.substring(i + before.len(), s.len())
      }
      None => {
        parts = parts.append(s)
        break
      }
    }
  }

  "".join(parts)
}