Reputation: 3908
I have a string like this @"Root/dbo-NewsLocation/Item 1/dbo-Relator-test/Item 1/Item 1"
When user taps on back button I need to remove @"/Item1" at the end of above string.
I am not able to remove @"Item1" If I do the stringByReplacingOccuranceOfString
by using below code it remove all @"Item1" using the below code.
NSString *str1=self.lblCIPath.text;
NSRange range = [str1 rangeOfString:@"/" options: NSBackwardsSearch];
NSString *newString = [str1 substringFromIndex:(range.location)];
self.lblCIPath.text=[self.lblCIPath.text stringByReplacingOccurrencesOfString:newString withString:@""];
I will be thankful if anyone can suggest me the way to delete the last componentPath
from NSString
.
Upvotes: 0
Views: 457
Reputation: 4143
As Mathieu Meylan suggested you can use stringByDeletingLastPathComponent
, or you can simply adjust your code to use substringToIndex
instead of substrinFromIndex
as follows:
NSString *str1=self.lblCIPath.text;
NSRange range = [str1 rangeOfString:@"/" options: NSBackwardsSearch];
NSString *newString = [str1 substringToIndex:(range.location)];
Final result will be the same:
Root/dbo-NewsLocation/Item 1/dbo-Relator-test/Item 1
Upvotes: 3
Reputation: 1148
Have you tried the method stringByDeletingLastPathComponent
?
NSString *updatedPath = [myFullPath stringByDeletingLastPathComponent];
Upvotes: 7