felixwcf felixwcf - 23 days ago 8
iOS Question

Get all characters after the last '/' (slash) from url string

How to get all characters after the last '/' (slash) from url string?

I receive a json string from my URL scheme when making webview request for certain site. For example:


app://ga/ecommerce/%7B%22product%22:%22playstation4%22...}]


I would like to grab the substring after the last '/'(slash).

How can I achieve it? May I know what is the format of Regex?

Avoid to use

NSRange lastDotRange = [sURL rangeOfString:@"/" options:NSBackwardsSearch];


because I might have escaped '/' in my json.

Thank you.

Answer Source

Well it seems like using Regex is quite expensive inside shouldStartLoadWithRequest delegate especially if you're having hybrid app which has plenty webviews. Some of my websites in webview might have more than one request, some are running at the background. It's painful if the webview triggers regex codes everytime when webview loads the request.

And also, I have escaped '/' in my last component params (json string), it might cause the lastComponent breaks after the escaped '/' character.

Therefore I stick to the codes filtering by using if-else statements and also compare strings with components of URL For example

request.URL.absoluteString
request.URL.host
request.URL.fragment

and also found out that @Nitin Gohel is useful.