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:


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


and also found out that @Nitin Gohel is useful.