2010-10-08 4 views

Répondre

2
NSString *newString = [(NSString *)yourOldString stringByReplacingOccurrencesOfString:@"anystring" withString:@""]; 

Sinon, vous allez devoir obtenir une copie de REGEXKitLite et utiliser une fonction regex comme:

NSString *newString = [(NSString *)yourOldString stringByReplacingOccurrencesOfRegex:@"ab\\b.\\byz" withString:@"ab yz"]; 
+0

Merci Thomas.I essayé premier mais cela ne work.I suis pas clair sur la deuxième réponse. Dans mon cas, la chaîne entre "ab" et "yz" n'est pas fixe longueur chaîne.aide moi si c'est possible. –

+0

afaik a fullstop '.' dans regex signifie" n'importe quelle quantité de caractères ". Vous devez télécharger regexKitLite et l'inclure dans votre projet. –

+0

Bonjour Thomas Merci encore. Je l'ai essayé. Pas d'erreur de compilation mais peut-être une erreur de logique. J'ai mis le code. Dites-moi si vous avez une suggestion pour cela. NSString * str = [[NSString alloc] init]; str = @ "ab Ceci est démo yz"; NSString * newstr = [[NSString alloc] init]; newstr = [str chaîneByReplacingOccurrencesOfRegex: @ "ab \\ b. \\ byz" avec Chaîne: @ "bon"]; –

4

le code est testé ici pour votre tâche.

NSString *string = @"ab anystring yz"; 
NSString *result = nil; 
// Determine "ab" location 
NSRange divRange = [string rangeOfString:@"ab" options:NSCaseInsensitiveSearch]; 
if (divRange.location != NSNotFound) 
{ 
    // Determine "ab" location according to "yz" location 
    NSRange endDivRange; 

    endDivRange.location = divRange.length + divRange.location; 
    endDivRange.length = [string length] - endDivRange.location; 
    endDivRange = [string rangeOfString:@"yz" options:NSCaseInsensitiveSearch range:endDivRange]; 

    if (endDivRange.location != NSNotFound) 
    { 
     // Tags found: retrieve string between them 
     divRange.location += divRange.length; 
     divRange.length = endDivRange.location - divRange.location; 

     result = [string substringWithRange:divRange]; 

     string =[string stringByReplacingCharactersInRange:divRange withString:@"Replace string"]; 
    } 
} 

Maintenant, vous vérifiez que la chaîne que vous obtiendrez ab Remplacer la chaîne yz

-1
-(NSString*)StrReplace :(NSString*)mainString preMatch:(NSString*) preMatch postMatch:(NSString*) postMatch replacementString:(NSString*) replacementString 
    { 
     @try 

     { 

      if (![mainString isEqualToString:@""] || [mainString isKindOfClass:[NSNull class]] || mainString==nil) 
      { 


       NSArray *preSubstring = [mainString componentsSeparatedByString:preMatch]; 
       NSString *preStr = [preSubstring objectAtIndex:0]; 

       NSArray *postSubstring = [mainString componentsSeparatedByString:postMatch]; 
       NSString *postStr = [postSubstring objectAtIndex:1]; 

       NSString *resultStr = [NSString stringWithFormat:@"%@%@%@%@%@" ,preStr,preMatch,replacementString,postMatch,postStr]; 

       return resultStr; 

      } 
      else 
      { 
       return @""; 
      } 

     } 
     @catch (NSException *exception) { 
     } 
    } 
+0

S'il vous plaît modifier votre question: (i) ajouter une _explanation_ à votre code (juste le code n'est pas une réponse à une question 'est-ce possible') et (ii) fixer votre _code block_ (car tout le code est dans le code bloc). –

Questions connexes