Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append to beginning of NSString method

I would like to append NSString A in front of NSString B. Is there a built in method to append to the beginning of a NSString instead of the end of the NSString?

I know that I can use stringWithFormat, but then what is the difference between using stringWithFormat and stringByAppendingString to add text to the end of a NSString?

like image 636
ansonl Avatar asked Feb 02 '13 21:02

ansonl


1 Answers

If you can append to the end of a string, you can prepend to the beginning of the string.

Append

NSString* a = @"A";
NSString* b = @"B";
NSString* result = [a stringByAppendingString:b]; // Prints "AB"

Prepend

NSString* a = @"A";
NSString* b = @"B";
NSString* result = [b stringByAppendingString:a]; // Prints "BA"
like image 183
WDUK Avatar answered Sep 19 '22 15:09

WDUK