【发布时间】:2009-03-02 12:25:46
【问题描述】:
我想添加一些标记来分隔一些字符串。 如何将字符添加到字符串中?
例如在“Hello”中添加'\x01',在“World”之前添加'\x02',在“World”之后添加'\x03'。
所以我可以创建一个字符串“\x01 Hello \x02 World \x03”,它有一些单独的标记。
【问题讨论】:
标签: iphone objective-c cocoa string
我想添加一些标记来分隔一些字符串。 如何将字符添加到字符串中?
例如在“Hello”中添加'\x01',在“World”之前添加'\x02',在“World”之后添加'\x03'。
所以我可以创建一个字符串“\x01 Hello \x02 World \x03”,它有一些单独的标记。
【问题讨论】:
标签: iphone objective-c cocoa string
如果你想修改一个字符串,你必须使用NSMutableString而不是NSString。如果您想从头开始创建字符串,则不需要。
例如,您可能想使用+stringWithFormat: 方法:
NSString * myString = [NSString stringWithFormat:@"%c %@ %c %@ %c",
0x01,
@"Hello",
0x02,
@"World",
0x03];
【讨论】:
嗯..
你可以这样做:
NSString *hello = @"hello";
char ch [] = {'\x01'};
hello = [hello stringByAppendingString:[NSString stringWithUTF8String:(char*)ch]];
我制作了一个 char* 来附加你的单个 char 并使用 stringWithUTF8String 来添加它。
不过,可能有一种不那么冗长的解决方法!
尼克。
【讨论】:
stringWithUTF8String 需要一个以空字符结尾的字符串,我相信,这不是我们这里所拥有的。
不完全确定您在问什么...但是 stringWithFormat 可能对您有帮助吗?
例如,
[NSString stringWithFormat:@"%c%@%c%@%c", 1, @"hello", 2, @"world", 3];
【讨论】: