-1

I have this string

let data = 123,456,7,8,9,10

I want to extract the last value separated by a "," which in this case would be 10, and its not necessarily a two digit value.

I tried this:

var data = 123,456,7,8,9,10
data = data.last!
Cœur
  • 37,241
  • 25
  • 195
  • 267
Lojas
  • 195
  • 3
  • 13

1 Answers1

2

Use String method data.components(separatedBy:)

let data = "123,456,7,8,9,10"
let lastComponent = data.components(separatedBy: ",").last
print(lastComponent)
Bilal
  • 18,478
  • 8
  • 57
  • 72