Search code examples
stringurlswift3

How to extract a string from URL in Swift 3?


I have a URL of a YouTube video. Now, I need to extract a string and save it to a variable. Here, I need to extract videoid from the URL. The link is:

https://www.youtube.com/watch?v=iTcq6L-PaDQ

Now, I need to extract iTcq6L-PaDQ in Swift 3.


Solution

  • Swift 3.0

    You can get Video ID as substring from String Url like below.

    let url = URL(string: "https://www.youtube.com/watch?v=iTcq6L-PaDQ" )!.absoluteString
    
    let Id = url.substring(from: url.range(of: "?v=")!.upperBound)