2009-11-22 6 views
1

Je suis nouveau au développement d'applications iphone. j'ai une chaîne dire abc, je veux juste l'afficher comme "Bonjour abc" à l'écranCordes en Objective C pour Iphone

je veux ajouter Bonjour à abc, avant abc. Dans c objectif, je l'ai vu fonctions appendString, qui affiche le résultat comme "abc Bonjour"
Mais je veux i afficher comme "Bonjour abc"

Répondre

3

La meilleure façon de le faire est:

NString *myString = @"abc"; 
NSString *finalString = [NSString stringWithFormat:@"Hello %@", myString]; 
NSLog(@"%@", finalString); 

cela affichera "Hello abc".

Je dis que c'est la meilleure façon, beacause vous pouvez réutiliser cette méthode pour ajouter plus de choses à la chaîne, comme un numéro:

NSString *playerName = @"Joe"; 
int num = 5; 
[NSString stringWithFormat:@"%@ scored %d goals.", playerName, 5]; 
1

Essayez de faire ceci:

NSString *string1 = @"abc"; 
NSString *string2 = [NSString stringWithFormat:@"Hello %@", string1]; 
0

Vous pas besoin de re-déclarer les variables NString vous pouvez le faire via une seule variable Mutable comme .....

NSMutableString *myStr = [NSMutableString string]; 
[myStr appendString:@"Hello"]; 

[myStr appendString:@" Cloud"]; 

int num = 9; 

[myStr appendFormat:@" Number %i",num]; 

NSLog(@"%@",myStr); 

et ce w Imprime "Hello Cloud Number 9".

Espérons que cela aide.