2017-05-03 1 views
0

Je veux couper l'apparition de caractères spéciaux dans une chaîneComment Tailler des caractères spéciaux dans une chaîne dans l'objectif c

String has :  
prevs: Case Number                  
____________________ 

Dans ce que je veux supprimer -------- ce tableau de bord de cette chaîne .J'ai essayé comme ceci:

NSCharacterSet *trim = [NSCharacterSet characterSetWithCharactersInString:@"-"]; 
    NSString *stringNew = [[previousString2 componentsSeparatedByCharactersInSet:trim] componentsJoinedByString:@""]; 

Merci à l'avance!

Répondre

1

Essayez ceci:

NSString *stringWithoutDash = [yourString stringByReplacingOccurrencesOfString:@"-" withString:@""]; 
+0

@ Merci, son fonctionnement –

+0

@SaraswathiAgal :) Codage Heureux .. –

0
NSString *trimedString = [myString stringByReplacingOccurrencesOfString:@"-" withString:@""]; 
0

Utiliser une expression régulière, le motif [\\s_]{4,} recherches pour 4 et plus un espace ou un trait de soulignement caractères.

NSString *trimmedString = [previousString2 stringByReplacingOccurrencesOfString:@"[\\s_]{4,}" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, previousString2.length)]; 

Si le caractères sont soulignement des tirets vraiment reaplace le caractère dans le modèle.

0

Vous pouvez utiliser le code ci-dessous pour supprimer votre chaîne spéciale.

NSString *yourString = @"This is your string with speical character --------"; 
NSCharacterSet *removedCharacterSet = [NSCharacterSet characterSetWithCharactersInString:@"--------"]; 
NSString *finalString = [[yourString componentsSeparatedByCharactersInSet:removedCharacterSet] componentsJoinedByString:@""]; 
NSLog (@"Your final string : %@", finalString);