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.
First, find the last index of ('/') using . lastIndexOf(str) method. Use the . substring() method to get the access the string after last slash.
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.
You can just use substr() for this.
For Swift 3 try this.
let fileName = "your/file/name/here"
let fileArray = fileName?.components(separatedBy: "/")
let finalFileName = fileArray?.last
Output : "Here"
try this:
NSURL *url = [NSURL URLWithString:@"app://ga/ecommerce/%7B%22product%22:%22playstation4%22..."];
NSString *last = [url lastPathComponent];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With