Get the App
SLTechnology News&Howtos  ›  Network Security  › 

Get the character after the last'/'of URL

Shulou Source: shulou.com Published: 2022-06-01 05:36:28 10月04日 Update

In the process of development projects, we often encounter problems that need to parse the URL of forums, blogs, etc., such as: 'abc/def/jkl' or' abc/def/jkl/', gets the last'/ 'after all the characters' jkl', because the number of special characters'/'is not fixed, front-to-back forward parsing URL is a bit difficult, in fact, there is a simpler way, that is reverse parsing.

The idea of reverse parsing is very simple, because the position of the last special character'/'is uncertain, so use the reverse function to convert the last special character'/ 'into the first special character, and get all the characters before the first special character' /'.

Script1 for URL that does not end with'\'

Declare @ ExpressionToSearch varchar (max) set @ ExpressionToSearch='/eeabc/def/abc/jkl'--set @ ExpressionToSearch='eeabc/def/abc/jkl'--select reverse (@ ExpressionToSearch) select right (@ ExpressionToSearch, iif (charindex ('/', reverse (@ ExpressionToSearch), 1) = 0, len (@ ExpressionToSearch), charindex ('/', reverse (@ ExpressionToSearch), 1)-1)

Script2, if it ends with'/', take the string between the last two'/ 'characters, similar to taking' jkl' 'from the string' abc/def/jkl/''

Declare @ ExpressionToSearch varchar (max) set @ ExpressionToSearch='/eeabc/def/abc/jkl/'--set @ ExpressionToSearch='eeabc/def/abc/jkl'--select reverse (@ ExpressionToSearch) select left (StrToSearch,len (StrToSearch)-charindex ('/', reverse (StrToSearch), 1)) from (select right (@ ExpressionToSearch, iif (charindex ('/', reverse (@ ExpressionToSearch), 2) = 0, len (@ ExpressionToSearch)) Charindex ('/', reverse (@ ExpressionToSearch), 2)-1)) as T (StrToSearch)

Tags: Characters special string difficulty two number between position function blog that is ideas methods forums processes problems projects flexibility development Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Shulou Information Microsoft NVidia Xiaomi