Commonly used string methods, Creating strings – Apple WebObjects 3.5 User Manual

Page 192

Advertising
background image

Chapter 11

WebScript Programmer’s Quick Reference to Foundation Classes

192

Commonly Used String Methods

The following sections list the most commonly used NSString and
NSMutableString methods, grouped according to function.

Creating Strings

The methods for creating strings are class methods, denoted by the plus sign
(+). You use class methods to send messages to a class—in this case, NSString
and NSMutableString. For more information on class methods, see “Sending a
Message to a Class” (page 172).

+ string

Returns an empty string. Usually used to create NSMutableStrings.
NSStrings created with this method are permanently empty.

/* Most common use */
id mutableString = [NSMutableString string];

/* May not be what you want */
id string = [NSString string];

+ stringWithFormat:

Returns a string created by substituting arguments into a specified
format string just as

printf()

does in the C programming language. In

WebScript, only the at sign (@) conversion character is supported, and
it expects a corresponding

id

argument.

// 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];

+ stringWithString:

Returns a string containing the same contents as a specified string.
This method is usually used to create an NSMutableString from an
NSString. For example, the following statement creates an
NSMutableString from a constant NSString object:

id mutableString = [NSMutableString stringWithString:@"Change me."];

Advertising