Copy a string: Difference between revisions

Objective C, various string copying options
(→‎[[Toka]]: Updated to work with Toka R1)
(Objective C, various string copying options)
Line 55:
strncpy(dst, src, 80);
dst[79] = 0;
 
==[[Objective-C]]==
[[Category:Objective-C]]
 
Immutable strings - since they are immutable, you may get the same instance with its references count increased.
 
NSString *original = @"Literal String"
NSString *new = [original copy];
 
Mutable strings - you can get either new mutable or immutable string:
 
NSMutableString *original = @"Literal String"
NSString *immutable = [original copy];
NSMutableString *mutable = [original mutableCopy];
 
Copying a CString:
 
char *cstring = "I'm a plain C string";
NSString *string = [NSString stringWithUTF8String:cstring];
 
Copying from data, possibly not null terminated:
 
char bytes[] = "some data";
NSString *string = [[NSString alloc] initWithBytes:bytes length:9 encoding:NSASCIIStringEncoding];
 
And of course, if a C string is needed, you can use standard functions like strcpy.
 
==[[C plus plus|C++]]==
Anonymous user