iphoneobjective-ccocoastring

How to concatenate char and string on iPhone?


I want to add some marks to separate some strings. How to add a char to a string?

e.g. add '\x01' to "Hello", add '\x02' before "World" and add '\x03' after "World".

So I can create a string "\x01 Hello \x02 World \x03" which has some separate marks.


Solution

  • If you want to modify a string, you have to use NSMutableString instead of NSString. There is no such need if you want to create a string from scratch.

    For instance, you may want to use +stringWithFormat: method:

    NSString * myString = [NSString stringWithFormat:@"%c %@ %c %@ %c",
                                                     0x01,
                                                     @"Hello",
                                                     0x02,
                                                     @"World",
                                                     0x03];