
Commonly Used String Methods
193
+ stringWithContentsOfFile:
Returns a string created by reading characters from a specified file.
For example, the following statement creates an NSString
containing the contents of the file specified in
path:
id fileContents = [NSString stringWithContentsOfFile:path];
See also writeToFile:atomically: (page 195) .
Combining and Dividing Strings
– stringByAppendingFormat:
Returns a string made by appending to the receiver a string
constructed from a specified format string and the arguments
following it in the manner of
stringWithFormat:. For example, assume
the variable
guestName contains the string “Rena”. Then the
following code excerpt produces the string
message with contents
“Hi, Rena!”:
id string = @"Hi";
id message = [string stringByAppendingFormat:@", %@!",
guestName];
– stringByAppendingString:
Returns a string made by appending a specified string to the
receiver. This code excerpt, for example, produces the string
“Error: premature end of file.”:
id errorTag = @"Error: ";
id errorString = @"premature end of file.";
id errorMessage = [errorTag
stringByAppendingString:errorString];
– componentsSeparatedByString:
Returns an NSArray containing substrings from the receiver that
have been divided by a specified separator string. For example,
the following statements produce an NSArray containing the
strings “wrenches”, “hammers”, and “saws”:
id toolString = @"wrenches, hammers, saws";
id toolArray = [toolString componentsSeparatedByString:@", "];
See also componentsJoinedByString: (page 200).
– substringToIndex:
Returns a string object containing the characters of the receiver up
to, but not including, the one at the specified index.
Kommentare zu diesen Handbüchern