How to concatenate char and string on iPhone?
Solution 1
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];
Solution 2
Hm..
You could do something like this:
NSString *hello = @"hello";
char ch [] = {'\x01'};
hello = [hello stringByAppendingString:[NSString stringWithUTF8String:(char*)ch]];
I make a a char* to append out of your single char and use stringWithUTF8String to add it.
There's probably a less long-winded way of solving it however!
Nick.
Solution 3
Not exactly sure what you're asking... But would stringWithFormat maybe help you?
E.g,
[NSString stringWithFormat:@"%c%@%c%@%c", 1, @"hello", 2, @"world", 3];
Chilly Zhong
I'm a mobile app developer. I was developing for Windows Mobile and Blackberry. I begin to dig into iPhone Development since Feb, 2009. Working has become a brand-new page for me and for my life. I was working on a symbian app for a short time in March, 2010. And I begin to learn Android in Nov, 2010 and learn WindowsPhone in Nov, 2011. Now I'm working on an iOS framework similar to iAd and I'm happy to offer all kinds of libs to help other developers. Always walking down your own way and keep your faith.
Updated on June 06, 2022Comments
-
Chilly Zhong almost 2 years
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.
-
Jean-Denis Muys over 7 years
stringWithUTF8String
expects a null-terminated string, I believe, which is not what we have here.