code.cpp 361 B

12345678
  1. /* Given a YouTube URL, extract the video ID and return it as a string.
  2. */
  3. std::string youtubeId(std::string link) {
  4. // YouTube ID is either the "v" query parameter or a path
  5. std::size_t v = link.find("v=");
  6. // If it is the path, then it is just the last 11 characters.
  7. return std::string::npos == v ?link.substr(link.size()-11) :link.substr(v+2,11);
  8. }