Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extract substring from NSString [closed]

I wants to extract substring from NSString. How it is possible.

Example : String is abcdefwww.google.comabcdef

output : www.google.com

In the main String, It is not predefined which url comes, It may be google.com or yahoo.com etc.

So I have to find the Start location of w in www and the the last location of m in .com.

So my question is that how I can find the start location of w in www and the location of m in .com, so i extract this part

Edited due to issue :

How we can extract www.mywebsite.co.uk from this string asdfwww.mywebsite.co.ukasdfajsf

Thanks

like image 573
QueueOverFlow Avatar asked Nov 16 '12 11:11

QueueOverFlow


1 Answers

substring from string like bellow

       - (NSString *)extractString:(NSString *)fullString toLookFor:(NSString *)lookFor skipForwardX:(NSInteger)skipForward toStopBefore:(NSString *)stopBefore 
    {

        NSRange firstRange = [fullString rangeOfString:lookFor];
        NSRange secondRange = [[fullString substringFromIndex:firstRange.location + skipForward] rangeOfString:stopBefore];
        NSRange finalRange = NSMakeRange(firstRange.location + skipForward, secondRange.location + [stopBefore length]);

        return [fullString substringWithRange:finalRange];
    }

use this method like below...

NSString *strTemp = [self extractString:@"abcdefwww.google.comabcdef" toLookFor:@"www" skipForwardX:0 toStopBefore:@".com"];
NSLog(@"\n\n Substring ==>> %@",strTemp);

for more information see bellow link..

help-needed-function-extract-string-string

like image 88
Paras Joshi Avatar answered Oct 16 '22 05:10

Paras Joshi