/* Most common use */ id mutableString = [NSMutableString string]; /* May not be what you want */ id string = [NSString string];
// These are fine id party = [NSString stringWithFormat:@"Party date: %@", partyDate]; id mailto = [NSString stringWithFormat:@"mailto: %@", [person email]]; id footer = [NSString stringWithFormat: @"Interaction %@ in session %@.", numberOfInteractions, sessionNumber]; // NO! This won't work. Only %@ is supported. // (%d prints address, not value). id string = [NSString stringWithFormat:@"%d of %d %s", x, y, cString];
id mutableString = [NSMutableString stringWithString:@"Change me."];
id fileContents = [NSString stringWithContentsOfFile:path];- See also writeToFile:atomically: .
id string = @"Hi"; id message = [string stringByAppendingFormat:@", %@!", guestName];
id errorTag = @"Error: "; id errorString = @"premature end of file."; id errorMessage = [errorTag stringByAppendingString:errorString];
id toolString = @"wrenches, hammers, saws"; id toolArray = [toolString componentsSeparatedByString:@", "];- See also componentsJoinedByString:.
if ([@"hello" compare:@"Hello"] == -1) { result = [NSString stringWithFormat: @"'%@' precedes '%@' lexicographically.", @"hello", @"Hello"]; }
if ([string isEqual:newString]) { result = @"Found a match"; }- assign the contents "Found a match" to result if string and newString are lexicographically equal.
id message = [NSMutableString stringWithString:@"Hi"]; [message appendFormat:@", %@!", guestName];
id mutableString = [NSMutableString stringWithFormat:@"Hello "]; [mutableString appendString:@"world!"];- mutableString has the resulting contents "Hello world!".
[mutableString setString:@""];
id errorLog = [NSString stringWithContentsOfFile:errorPath]; id newErrorLog = [errorLog stringByAppendingFormat:@"%@: %@.\n", timeStamp, @"premature end of file."]; [newErrorLog writeToFile:errorPath atomically:YES];
Table of Contents Next Section