Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 838
felixwcf Avatar asked Mar 23 '16 10:03

felixwcf


People also ask

How do you find the last part of a URL after a slash?

First, find the last index of ('/') using . lastIndexOf(str) method. Use the . substring() method to get the access the string after last slash.

How do I get substring after last?

To get the value of a string after the last slash, call the substring() method, passing it the index, after the last index of a / character as a parameter. The substring method returns a new string, containing the specified part of the original string.

How do I get the last character of a URL?

You can just use substr() for this.


2 Answers

For Swift 3 try this.

let fileName = "your/file/name/here"
let fileArray = fileName?.components(separatedBy: "/")
let finalFileName = fileArray?.last

Output : "Here"

like image 110
9BallOnTheSnap Avatar answered Oct 29 '22 14:10

9BallOnTheSnap


try this:

    NSURL *url = [NSURL URLWithString:@"app://ga/ecommerce/%7B%22product%22:%22playstation4%22..."];
    NSString *last = [url lastPathComponent];
like image 41
winner.ktw Avatar answered Oct 29 '22 14:10

winner.ktw