Copied to clipboard

Flag this post as spam?

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


  • Kim Andersen 1447 posts 2196 karma points MVP
    Dec 05, 2014 @ 14:04
    Kim Andersen
    0

    substring-after and substring-before in Razor?

    Hi all

    I have a question about substring-after and substring-before that I'm used to use in XSLT, but now I have to use the same feature in Razor in Umbraco 7.

    Let's say I have a link from YouTube that looks like this: https://www.youtube.com/watch?v=tF03jgCgV7s

    If I just want to grab the ID of the video (the parts that comes after the ?v=) in that string I would do something like this in XSLT:

    <xsl:variable name="rawId" select="substring-after('https://www.youtube.com/watch?v=tF03jgCgV7s','=')" />
    
    <xsl:variable name="videoId">
        <xsl:choose>
            <xsl:when test="contains($rawId, '&amp;')">
                <xsl:value-of select="substring-before($rawId, '&amp;')" />
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$rawId" />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    

    The second check in the videoId-variable is used to make sure that there's not any extra parameters in the URL. Something like a URL looking like this:

    https://www.youtube.com/watch?v=tF03jgCgV7s&featured=true

    How can I achieve the same as above in Razor?

    Thanks in advance.

    /Kim A

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Dec 05, 2014 @ 14:13
    Jan Skovgaard
    0

    Hi Kim

    I don't think there is such same thing in Razor - But perhaps http://www.dotnetperls.com/substring can help a bit? Otherwise the way forward may be using regex like Warren shows an example of in this rather old thread? http://our.umbraco.org/forum/developers/razor/26473-substring-before-substring-after

    Some of the backend guys'n'gals at the office must have an idea about it otherwise then I think since it's just c#, right? :)

    You can probably also look into using "indexOf" in combination with substring.

    Hope this helps.

    /Jan

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Dec 05, 2014 @ 14:33
    Dennis Aaen
    100

    Hi Kim :-)

    Try to see JeavonĀ“s suggestion here maybe you could use the same approach http://our.umbraco.org/forum/developers/razor/56828-Extract-Youtube-ID-from-url

    Hope this helps,

    /Dennis

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Dec 05, 2014 @ 21:28
    Dennis Aaen
    0

    Hi Kim :-)

    Or maybe you can get some inspiration from this post here http://our.umbraco.org/forum/umbraco-7/using-umbraco-7/54749-Youtube-Embed

    /Dennis

  • Kim Andersen 1447 posts 2196 karma points MVP
    Dec 08, 2014 @ 09:11
    Kim Andersen
    2

    Hi guys

    I used the trick from the post Dennis linked to, where Jeavon had a great answer. It solved my issue in this case.

    If anyone else is interested in the answer in this case, the following code (more or less copied from the other thread) solved it for me:

    var uri = new Uri(CurrentPage.youtubeTopVideo);
    var query = HttpUtility.ParseQueryString(uri.Query);
    
    var youTubeId = query.Get("v");
    

    Thanks!

    /Kim A

Please Sign in or register to post replies

Write your reply to:

Draft