Callum
Callum

Reputation: 3381

Pulling the Video ID of a YouTube video using substrings

I am currently trying to extract the ID of a YouTube video from the embed url YouTube supplies.

I am currently using this as an example:

<iframe width="560" height="315" src="http://www.youtube.com/embed/aSVpBqOsC7o" frameborder="0" allowfullscreen></iframe>

So far my code currently looks like this,

 else if (TB_VideoLink.Text.Trim().Contains("http://www.youtube.com/embed/"))
        {
            youtube_url = TB_VideoLink.Text.Trim();
            int Count  = youtube_url.IndexOf("/embed/", 7);
            string cutid = youtube_url.Substring(Count,youtube_url.IndexOf("\" frameborder"));

            LB_VideoCodeLink.Text = cutid;
        }

I Seem to be getting there, however the code falls over on CutID and I am not sure why???

Cheers

Upvotes: 2

Views: 1425

Answers (3)

danswain
danswain

Reputation: 4177

Similar to answer above, but was beaten to it.. doh

            //Regex with YouTube Url and Group () any Word character a-z0-9 and expect 1 or more characters +
        var youTubeIdRegex = new Regex(@"http://www.youtube.com/embed/(?<videoId>\w+)",RegexOptions.IgnoreCase|RegexOptions.Compiled);

        var youTubeUrl = TB_VideoLink.Text.Trim();

        var match = youTubeIdRegex.Match(youTubeUrl);
        var youTubeId = match.Groups["videoId"].Value; //Group[1] is (\w+) -- first group ()

        LB_VideoCodeLink.Text = youTubeId;

Upvotes: 0

Lukazoid
Lukazoid

Reputation: 19426

I always find it much easier to use regular expressions for this sort of thing, Substringand IndexOf always seem dated to me, but that's just my personal opinion.

Here is how I would solve this problem.

Regex regexPattern = new Regex(@"src=\""\S+/embed/(?<videoId>\w+)");
Match videoIdMatch = regexPattern.Match(TB_VideoLink.Text);

if (videoIdMatch.Success)
{
    LB_VideoCodeLink.Text = videoIdMatch.Groups["videoId"].Value;
}

This will perform a regular expression match, locating src=", ignoring all characters up until /embed/ then extracting all the word characters after it as a named group.

You can then get the value of this named group. The advantage is, this will work even if frameborder does not occur directly after the src.

Hope this is useful,

Luke

Upvotes: 7

tonycoupland
tonycoupland

Reputation: 4247

The second parameter of the Substring method is length, not second index. Subtract the index of the second test from the first to get the required length.

else if (TB_VideoLink.Text.Trim().Contains("http://www.youtube.com/embed/"))
    {
        youtube_url = TB_VideoLink.Text.Trim();
        // Find the start of the embed code
        int Count  = youtube_url.IndexOf("/embed/", 7);
        // From the start of the embed bit, search for the next "
        int endIndex = youtube_url.IndexOf("\"", Count);
        // The ID is from the 'Count' variable, for the next (endIndex-Count) characters
        string cutid = youtube_url.Substring(Count, endIndex - Count);

        LB_VideoCodeLink.Text = cutid;
    }

You probably should have some more exception handling for when either of the two test strings do not exist.

Upvotes: 2

Related Questions