2009-06-08 9 views
30

Comment peut-on remplir une chaîne à gauche dans l'objectif c. si j'ai par exemple une valeur entière de 6 Je veux qu'il soit affiché comme 06.Chaîne de remplissage à gauche

J'utilise stringByPaddingToLength: mais il pads à droite comme 60.

Votre aide est très appréciée.

Répondre

20
+ (NSString *)formatValue:(int)value forDigits:(int)zeros { 
    NSString *format = [NSString stringWithFormat:@"%%0%dd", zeros]; 
    return [NSString stringWithFormat:format,value]; 
} 
+0

Que faire si je veux ajouter 0 à gauche d'une chaîne au lieu d'un entier? Donc, si j'ai bonjour, je veux 000hello? – hzxu

+0

Merci !!!!!))))) – Alex

+2

Au lieu de construire un format variable, pourquoi ne pas simplement utiliser la longueur du champ variable, par ex. '[NSString stringWithFormat: @"% 0 * d ", zéros, valeur];'? – dreamlax

76

Celui-ci est tamponné à gauche avec 10 zéros.

NSString *padded = [NSString stringWithFormat:@"Padded left with zeros: %010d", 65]; 
+0

si 65 était un NSString? Existe-t-il un formateur pour gérer cela sans faire de conversion de type? –

13

Mal à l'aise, mais ça va faire l'affaire.

@implementation NSString (LeftPadding) 

- (NSString *)stringByPaddingTheLeftToLength:(NSUInteger)newLength withString:(NSString *)padString startingAtIndex:(NSUInteger)padIndex 
{ 
    if ([self length] <= newLength) 
     return [[@"" stringByPaddingToLength:newLength - [self length] withString:padString startingAtIndex:padIndex] stringByAppendingString:self]; 
    else 
     return [[self copy] autorelease]; 
} 

@end 

Ensuite, vous pouvez faire:

NSString *test1 = [@"6" stringByPaddingTheLeftToLength:10 withString:@"0" startingAtIndex:0]; 
// test1 = "0000000006" 

NSString *test2 = [@"asdf" stringByPaddingTheLeftToLength:10 withString:@"qwer" startingAtIndex:0]; 
// test2 = "qwerqwasdf" 

NSString *test3 = [@"More than ten characters" stringByPaddingTheLeftToLength:10 withString:@"bamboo" startingAtIndex:0]; 
// test3 = "More than ten characters" 

NSString *test4 = [test3 stringByPaddingTheLeftToLength:100 withString:test2 startingAtIndex:0]; 
// test4 = "qwerqwasdfqwerqwasdfqwerqwasdf...qwerqMore than ten characters" 
+0

Cela fonctionne très bien quand "aString" est "self" à la place. Merci. – TPoschel

+1

Pas maladroit du tout! Surtout quand l'objet à rembourrer est une chaîne, pas un nombre. BTW, le paramètre 'startingAtIndex' n'est plus nécessaire. –

7

fil vieux, je sais, mais cela est un moyen pratique de la jambière gauche. Je l'ai ma classe Util. Vous pouvez l'adapter au bon pad si nécessaire. La meilleure partie est que cela fonctionne avec n'importe quel rembourrage que vous voulez.

+ (NSString *) leftPadString:(NSString *)s withPadding:(NSString *)padding { 
    NSString *padded = [padding stringByAppendingString:s]; 
    return [padded substringFromIndex:[padded length] - [padding length]]; 
} 

Conforme aux normes ARC et non-ARC. :) Pour l'appeler, utilisez [Util leftPadString:@"42" withPadding:@"0000"];.

Vous pouvez également le mettre dans une catégorie sur NSString pour un appel encore plus facile comme [@"42" stringByLeftPadding:@"0000"].

Les deux vous 0042.

+0

Merci, et la chaîne de droite est là pour tous ceux qui le veulent: - (NSString *) rightPadString: (NSString *) s avecPadding: (NSString *) padding { NSString * padded = [s stringByAppendingString: padding]; return [padded substringToIndex: [longueur de remplissage]]; } – lppier

4

pour complétude (en utilisant * espace réservé):

NSString *paddedNumberString = [NSString stringWithFormat:@"%0*d", amountOfZeros, numberNSInteger] 
Questions connexes