-4

For example, this variable needs to add "25" after the "%" character. How can I do it?

var message = "cars %50 discount"

must be "cars %2550 discount"

rmaddy
  • 314,917
  • 42
  • 532
  • 579
emre kacan
  • 39
  • 3

2 Answers2

0

You can do that by replacing '%' with '%25' or whatever characters you'd like to insert after '%':

var message = "cars %50 discount"
message = message.replacingOccurrences(of: "%", with: "%25")

This approach is dangerous, check the this answer for more information and for an alternative approach.

Community
  • 1
  • 1
Mo Abdul-Hameed
  • 6,030
  • 2
  • 23
  • 36
-1

I believe this link provides the answer you need: How can I add Variables into a String?(Swift)

For your example do:

var discountAmount = 2550

var message = "cars %\(discountAmount) discount"

(and the message prints "cars %2550 discount"

Community
  • 1
  • 1
Mestwick
  • 41
  • 1
  • 1
  • 4