Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Michael 24 posts 137 karma points
    Dec 30, 2014 @ 14:54
    Michael
    0

    Split string after a certain character

    Hi guys

    I am working on a macro for responsive youtube video to use in the grid editor. The idea is that the editor enters the url and the macro does the rest. To get it to work I need to strip the url down to only contain everything after the = in youtube url's.
    From this: https://www.youtube.com/watch?v=erLk59H86ww
    To this: erLk59H86ww

    I thought I could do something like this:

    @if (Model.MacroParameters["ytVideoLink"] != null)
    {
      var fullLink = Model.MacroParameters["ytVideoLink"]; 
      var videoId = fullLink.Split('=').Last();
         
      <div class="videoWrapper">
        <iframe type="text/html" src="http://www.youtube.com/embed/@videoId" frameborder="0"></iframe>
      </div>
    }

    So far my search for some documentation on how to do a simple split has failed. Can anyone point my in the right direction?

    Happy New Year to you all :)

    /Michael

     

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Dec 30, 2014 @ 15:50
    Dennis Aaen
    1

    Hi Michael,

    Perhaps you can use the same approach that Jeavon shows in this thread. http://our.umbraco.org/forum/developers/razor/56828-Extract-Youtube-ID-from-url

    Hope this helps, or at least gives you inspiration

    /Dennis

  • Michael 24 posts 137 karma points
    Dec 30, 2014 @ 16:16
    Michael
    0

    Hi Dennis

    Thank you very much for pointing me in the right direction :) I worked out a solution based on the link you provided. My final solution looks like this:

    @if (Model.MacroParameters["ytVideoLink"] != null)
    {
      var fullLink = Model.MacroParameters["ytVideoLink"].ToString();
      var youTubeId = fullLink.Split('=').Last();

      <div class="videoWrapper">
        <iframe type="text/html" src="http://www.youtube.com/embed/@youTubeId" frameborder="0"></iframe>
      </div>
    }

    Thx again and happy New Year to you :)

    /Michael

Please Sign in or register to post replies

Write your reply to:

Draft