Tuesday, May 14, 2013

NSString initWithCharacters length example ios


[NSString initWithCharacters length]

Returns an initialized NSString object that contains a given number of characters from a given C array of Unicode characters.
- (id)initWithCharacters:(const unichar *)characters length:(NSUInteger)length
Parameters of [NSString initWithCharacters length]
characters
A C array of Unicode characters; the value must not be NULL.

Important: Raises an exception if characters is NULL, even if length is 0.
length
The number of characters to use from characters.
Return Value of [NSString initWithCharacters length]
An initialized NSString object containing length characters taken from characters. The returned object may be different from the original receiver.
Example of [NSString initWithCharacters length]
NSString *MyStringWithRandomUppercaseLetter(void) {
    unichar letter = MyRandomIntegerBetween(65, 90);
    return [[[NSString alloc] initWithCharacters:&letter length:1] autorelease];
}
Example of [NSString initWithCharacters length]
NSString col = @"1245DD";

unichar c1 = [col characterAtIndex:1];
unichar c2 = [col characterAtIndex:3];

int val1 = [[[NSString alloc] initWithCharacters:&c1 length:1] intValue];
int val2 = [[[NSString alloc] initWithCharacters:&c2 length:1] intValue];
Example of [NSString initWithCharacters length]
wchar_t *ws1 = va_arg(argListSave, wchar_t *);
wchar_t *ws2 = va_arg(argListSave, wchar_t *);

NSString *s1 = [[NSString alloc] initWithCharacters:ws1 length:wstrlen(ws1)];
NSString *s2 = [[NSString alloc] initWithCharacters:ws2 length:wstrlen(ws2)];

NSString *iosString = [[NSString alloc] initWithFormat:@"%@ = %@", s1, s2];
[s1 release];
[s2 release];