How to concatenate char and string on iPhone?

I want to add some labels to separate some lines. How to add char to string?

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

Therefore, I can create the string "\ x01 Hello \ x02 World \ x03" which has some separate labels.

+7
string objective-c iphone cocoa
source share
3 answers

If you want to change the string, you should use NSMutableString instead of NSString . There is no such need if you want to create a string from scratch.

For example, you can use the +stringWithFormat: method +stringWithFormat:

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

Hmm ..

You can do something like this:

 NSString *hello = @"hello"; char ch [] = {'\x01'}; hello = [hello stringByAppendingString:[NSString stringWithUTF8String:(char*)ch]]; 

I do a char * to add from your only char and use stringWithUTF8String to add it.

There is probably a shorter way to solve the problem.

Nick.

+2
source share

Not quite sure what you are asking ... But can stringWithFormat help you?

eg,

 [NSString stringWithFormat:@"%c%@%c%@%c", 1, @"hello", 2, @"world", 3]; 
+2
source share

All Articles