-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
  • 34,719
  • 24
  • 185
  • 251
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
  • 17,228
  • 8
  • 53
  • 69