I have a string "hi how are... you"
I want to find the Sub-string after how and before you..
How to do this in objective c?
I have a string "hi how are... you"
I want to find the Sub-string after how and before you..
How to do this in objective c?
Find the range of the two strings and return the substring in between:
NSString *s = @"hi how are... you";
NSRange r1 = [s rangeOfString:@"how"];
NSRange r2 = [s rangeOfString:@"you"];
NSRange rSub = NSMakeRange(r1.location + r1.length, r2.location - r1.location - r1.length);
NSString *sub = [s substringWithRange:rSub];
You could use the method of NSString substringWithRange
Example
NSString *string=@"hi how are you";
NSRange searchFromRange = [string rangeOfString:@"how"];
NSRange searchToRange = [string rangeOfString:@"you"];
NSString *substring = [string substringWithRange:NSMakeRange(searchFromRange.location+searchFromRange.length, searchToRange.location-searchFromRange.location-searchFromRange.length)];
NSLog(@"subs=%@",substring); //subs= are
use SubstringTOIndex
& SubstringFromIndex
functions of NSString
. Where SubstringFromIndex
gives you the string
from the index which you passed & SubstringToIndex
function gives you the string
upto the index which you passed.
Also try substringWithRange
function which returns you the string
between the range which you passed.
Use substringWithRange
...
NSString* substring = [originalString substringWithRange:NSMakeRange(3, originalString.length-6)];