2010-10-21 15 views

risposta

29

Questo dovrebbe anche fare il trucco:

NSString *result = [baseString stringByReplacingCharactersInRange:range withString:@""]; 
+0

Sì. Questo ha funzionato. Grazie!!! – Abhinav

3

È possibile ottenere la sottostringa all'inizio dell'intervallo e la sottostringa dalla fine dell'intervallo e concatenarle insieme.

NSString* stringByRemovingRange(NSString* theString, NSRange theRange) { 
    NSString* part1 = [theString substringToIndex:theRange.location]; 
    NSString* part2 = [theString substringFromIndex:theRange.location+theRange.length]; 
    return [part1 stringByAppendingString:part2]; 
} 

si potrebbe anche trasformarlo in un NSMutableString, e utilizzare the -deleteCharactersInRange: method che fa esattamente questo.

NSString* stringByRemovingRange(NSString* theString, NSRange theRange) { 
    NSMutableString* mstr = [theString mutableCopy]; 
    [mstr deleteCharactersInRange:theRange]; 
    return [mstr autorelease]; 
} 
-1

forse è possibile utilizzare questo pezzo di codice: Questo appare in una lista che ha un codice: a, b, c, d ,. .. e se l'ultimo codice è un d .. aggiungerà il codice e.

NSString *alphabet = @"ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
    NSUInteger i, count = [lead count]; 
    for (i = 0; i < count; i++) { 
     Listings * l = [lead objectAtIndex:i]; 
     NSUInteger location = [alphabet rangeOfString:l.code].location + 1; 
     if(!([l.code isEqualToString:@"X"])) 
     { 
      if(!(location -1 == i)) 
      { 
       NSRange range = {location - 2,1}; 
       NSString *newCode = [alphabet substringWithRange:range]; 
       l.code = newCode; 

      } 
     } 
+0

'lead'? 'Listings'? Ok, 8 anni dopo e il poster non è apparso sul sito da 7 anni ... ma cos'è questo casino? Né il codice in esso né la descrizione hanno nulla a che fare con la domanda che è stata posta. – mah

Problemi correlati