2011-04-22 4 views

Répondre

1

Vous pouvez utiliser la méthode

- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target 
             withString:(NSString *)replacement 

Donc, votre code devrait être comme ci-dessous.

string = [string stringByReplacingOccurrencesOfString:@"sdk.com" withString:@""]; 

ÉDITÉ:

Utilisez la méthode componentsSeparatedByString,

NSArray* myArray = [string componentsSeparatedByString:@"@"]; 

Maintenant, votre tableau contient exactement deux composants,

NSMutableString* myStringBefore = [myArray objectAtIndex:0]; 

si vous voulez toujours @ à la fin de votre chaîne ..

[myStringBefore appenString:@"@"]; 
+0

cette chaîne est juste un exemple de ce que je dois le faire peut-être [email protected] la solution doit être générale – Gypsa

+0

J'ai trouvé une solution int range = [chaîne rangeOfString: @ "@"]. Location; \t NSLog (@ "% d", intervalle); \t NSLog (@ "% @", [chaîne substringToIndex: plage]); – Gypsa

+0

@Jenifer: Bon, jetez un oeil à ma réponse mise à jour, – Jhaliya

1

Vous pouvez utiliser substringToIndex

string = [ string substringToIndex:5 ] ; 
// If @ location is subject to change, then first find it's location index and 
// pass it. 
1
NSMutableString *string1 = [NSMutableString stringWithString: @"[email protected]"]; 
NSArray* arr = [string1 componentsSeparatedByString:@"@"]; 
[string1 deleteCharactersInRange: [string1 rangeOfString: [NSString stringWithFormat:@"@%@",[arr objectAtIndex:1]]]]; 
+1

Pourquoi ne pas simplement '[[theString componentsSeparatedByString: @" @ "] objectAtIndex: 0]'? Pas besoin d'une chaîne mutable. –

0
int range=[string rangeOfString:@"@"].location; 
NSLog(@"%d",range); 
NSLog(@"%@",[string substringToIndex:range]);