NSString *[email protected]"[email protected]";
Maintenant, je veux supprimer tous les caractères de la chaîne de @ qui aboutissent à "test1". Veuillez me suggérer comment faire. Merci d'avance !!Supprimer la chaîne de @
NSString *[email protected]"[email protected]";
Maintenant, je veux supprimer tous les caractères de la chaîne de @ qui aboutissent à "test1". Veuillez me suggérer comment faire. Merci d'avance !!Supprimer la chaîne de @
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:@"@"];
Vous pouvez utiliser substringToIndex
string = [ string substringToIndex:5 ] ;
// If @ location is subject to change, then first find it's location index and
// pass it.
NSMutableString *string1 = [NSMutableString stringWithString: @"[email protected]"];
NSArray* arr = [string1 componentsSeparatedByString:@"@"];
[string1 deleteCharactersInRange: [string1 rangeOfString: [NSString stringWithFormat:@"@%@",[arr objectAtIndex:1]]]];
Pourquoi ne pas simplement '[[theString componentsSeparatedByString: @" @ "] objectAtIndex: 0]'? Pas besoin d'une chaîne mutable. –
int range=[string rangeOfString:@"@"].location;
NSLog(@"%d",range);
NSLog(@"%@",[string substringToIndex:range]);
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
J'ai trouvé une solution int range = [chaîne rangeOfString: @ "@"]. Location; \t NSLog (@ "% d", intervalle); \t NSLog (@ "% @", [chaîne substringToIndex: plage]); – Gypsa
@Jenifer: Bon, jetez un oeil à ma réponse mise à jour, – Jhaliya