2012-11-16 2 views
6

Desidero estrarre la sottostringa da NSString. Com'è possibile.sottostringa di estratto da NSString

Esempio: String è abcdefwww.google.comabcdef

uscita: www.google.com

nella stringa principale, non è predefinito che viene url, Può essere google. com o yahoo.com ecc

Quindi devo trovare la posizione di inizio di w in www e l'ultima posizione di m di .com.

Quindi la mia domanda è che come posso trovare la posizione di partenza di w in www e la posizione di m in .com, così ho estratto questa parte

modificato causata da problema :

Come possiamo estrarre www.mywebsite.co.uk da questa stringa asdfwww.mywebsite.co.ukasdfajsf

Grazie

+2

leggere NSString/NSMutableString documentazione –

+0

una breve ricerca su Google è tutto ciò che serve –

+0

@AndreyChernukha Ho curato la questione, si prega di controllare – QueueOverFlow

risposta

18

stringa dalla stringa come muggito

 - (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]; 
    } 

uso questo metodo come di seguito ...

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

per ulteriori informazioni si veda il link qui sotto ..

help-needed-function-extract-string-string

+0

grazie per la risposta .. Ho appena modificato la domanda, per favore controllala – QueueOverFlow

+0

grazie, sto cercando di implementare questo – QueueOverFlow

+0

c'è il problema Che ne dici di asdfwww.mywebsite.co.ukasdfajsf? – QueueOverFlow

8

È possibile ottenere sottostringa usando il seguente codice

-(NSString*)stringBetweenString:(NSString*)start andString:(NSString *)end withstring:(NSString*)str 
{ 
    NSScanner* scanner = [NSScanner scannerWithString:str]; 
    [scanner setCharactersToBeSkipped:nil]; 
    [scanner scanUpToString:start intoString:NULL]; 
    if ([scanner scanString:start intoString:NULL]) { 
     NSString* result = nil; 
     if ([scanner scanUpToString:end intoString:&result]) { 
      return result; 
     } 
    } 
    return nil; 
} 

Chiamare questo,

NSString *cURL=[self stringBetweenString:@"www." andString:@".com" withstring:@"abcdefwww.google.comabcdef"]; 

NSString *cAudiolink=[NSString stringWithFormat:@"www.%@.com",cURL]; 

cAudiolink è la stringa risultato

+0

Grazie per la tua risposta.è un problema su come estrarre questo asdfwww.mywebsite.co.ukasdfajsf – QueueOverFlow

2

È possibile utilizzare un'espressione regolare.

Utilizzando questa categoria:

@implementation NSString (MyRegex) 

- (NSString *)firstMatchWithRegex:(NSString *)regex error:(NSError **)e { 
    NSError *error = nil; 
    NSRegularExpression *re = [NSRegularExpression regularExpressionWithPattern:regex options:NSRegularExpressionSearch error:&error]; 

    if(re == nil) { 
     if(e) *e = error; 
     return nil; 
    } 

    NSArray *matches = [re matchesInString:self options:0 range:NSMakeRange(0, [self length])]; 

    if([matches count] == 0) {    
     NSString *errorDescription = [NSString stringWithFormat:@"Can't find a match for regex: %@", regex]; 
     NSError *error = [NSError errorWithDomain:NSStringFromClass([self class]) code:0 userInfo:@{NSLocalizedDescriptionKey : errorDescription}]; 
     if(e) *e = error; 
     return nil; 
    } 

    NSTextCheckingResult *match = [matches lastObject]; 
    NSRange matchRange = [match rangeAtIndex:1]; 
    return [self substringWithRange:matchRange]; 
} 

@end 

È possibile chiamare:

[@"abcdefwww.google.comabcdef" firstMatchWithRegex:@"(www.*\\.com)" error:nil] 

che restituisce:

www.google.com 
+0

Grazie per la risposta, come posso estrarre url mywebsite.co.uk da questa stringa asdfwww.mywebsite.co.ukasdfajsf – QueueOverFlow

+0

'[@" asdfwww.mywebsite.co.ukasdfajsf "firstMatchWithRegex: @" (www. * \\. co \\. uk) "errore: nil]' www.mywebsite.co.uk – nst

2

ho ottenuto la soluzione del mio problema, dopo googling.A implementare il codice qui sotto ed è il lavoro bene per me

NSString *finalURL; 
    NSString *[email protected]"abcdefwww.yahoomail.comabcdef"; 

    NSRange range = [string rangeOfString:@"www"]; 


    NSString *tempUrl=[string substringFromIndex:range.location]; 




    NSRange range2 = [tempUrl rangeOfString:@".com"]; 

    finalURL=[tempUrl substringWithRange:NSMakeRange(0, range2.location+4)]; 

    NSLog(@"your URL is %@",finalURL); //prints www.yahoomail.com