Hello everyone I am trying find a string inside a string lets say I have a string:
word1/word2/word3
I want to find the word from the end of the string to the last "/" so what I will get from that string is:
Word3
How do I do that? Thanks!
Hello everyone I am trying find a string inside a string lets say I have a string:
word1/word2/word3
I want to find the word from the end of the string to the last "/" so what I will get from that string is:
Word3
How do I do that? Thanks!
You are looking for the componentsSeparatedByString:
method
NSString *originalString = @"word1/word2/word3";
NSArray *separatedArray = [originalString componentsSeparatedByString:@"/"];
NSString *lastObject = [separatedArray lastObject]; //word3
once check this one By using this one you'l get last pathcomponent values,
NSString* theFileName = @"how /are / you ";
NSString *str1=[theFileName lastPathComponent];
NSLog(@"%@",str1);
By using lastPathComponent you'l get the last path component directly no need to take array for separate the string.
NSString *string = @"word1/word2/word3"
NSArray *arr = [string componentsSeperatedByString:@"/"];
NSSting *str = [arr lastObject];
you must use NSScanner class to split substring.
check this.
Objective C: How to extract part of a String (e.g. start with '#')
You can find it also with this way:
NSMutableString *string=[NSMutableString stringWithString:@"word1/word2/word3"];
NSRange range=[string rangeOfString:@"/" options:NSBackwardsSearch];
NSString *subString=[string substringFromIndex:range.location+1];
NSRegularExpression or NSString rangeOfString:options:range:locale: (with options to search backwards).
The answer really depends on exactly what the input string will contain (how consistent it is).