-3

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
  • 307,833
  • 40
  • 508
  • 550
emre kacan
  • 41
  • 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
  • 5,864
  • 2
  • 24
  • 34
-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