-1

I have a string like so:

let someString = "The (randomcharacters)(someknowncharacters) are playing in the NBA Finals"

I want to replace everything between the strings The and (some with the string Warriors. I have looked into using replacingOcurrences but that doesn't do what I want.

Slaknation
  • 2,124
  • 3
  • 23
  • 42
  • See https://stackoverflow.com/questions/44593934/better-way-to-replace-substring-between-indicators-in-swift?r=SearchResults&s=3|38.9663 – rmaddy Jun 04 '19 at 18:27
  • This question is a bit unclear now, is it always “Raptors” you want to replace or is that part also random? I assume there is a space before (somerandom...)? – Joakim Danielson Jun 04 '19 at 19:56
  • Is this a string you get at runtime, i.e. you can't just interpolate the strings? – Alexander Jun 04 '19 at 19:56
  • @JoakimDanielson There is no space. It is not always "Raptors" that I want to replace. What I want to do is be able to replace between two strings that are known. – Slaknation Jun 04 '19 at 19:59
  • So “somerandomcharacters” are not random then really? I think you need to give some examples and explain how they should be changed and what is known at compile time and what is known at runtime. – Joakim Danielson Jun 04 '19 at 20:03
  • @JoakimDanielson You are correct. I did not word this question well at all. I corrected it again. Now it should be understandable. Sorry... – Slaknation Jun 04 '19 at 20:09

2 Answers2

1

Using some "somerandomcharacters" would mean that you're essentially using a String as a (really shitty) vehicle to transport multiple pieces of data. But we already have a way to do that, with data types.

We can create a struct that holds the necessary information to describe a Basketball game. We can pass this data around throughout our app, with very easy access to its important components. Only our UI layer needs a string, so we only ever generate a string description of this struct at the very last moment, right at the UI layer.

struct BasketballTeam {
    var name: String
}

struct BasketballGame: CustomStringConvertible {
    let homeTeam: BasketballTeam
    let awayTeam: BasketballTeam
    let eventName: String

    var description: String {
        return "The \(homeTeam.name) are playing the \(awayTeam.name) in the \(eventName)."
    }
}

let game = BasketballGame(
    homeTeam: BasketballTeam(name: "Toronto Raptors"),
    awayTeam: BasketballTeam(name: "Golden State Warriors"),
    eventName: "NBA Finals"
)
print(game.description) // => The Toronto Raptors are playing the Golden State Warriors in the NBA Finals.
Alexander
  • 59,041
  • 12
  • 98
  • 151
1

Here is another attempt using replaceOccurencesOf

func replace(_ original: String, between firstPart: String, and secondPart: String, with: String ) -> String {
    let pattern = "\(firstPart) .* \(secondPart)"
    let replacement = "\(firstPart) \(with) \(secondPart)"
    return original.replacingOccurrences(of: pattern, with: replacement, options: .regularExpression)
}

It might need some adjustment in regard to handle space around the replaced word.

Example

let newString = replace("The Raptors are playing in the NBA Finals", between: "The", and: "are", with: "Warriors")
Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52