Get the character after the last'/'of URL
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)