Instance Method

components(separatedBy:)

Returns an array containing substrings from the receiver that have been divided by a given separator.

Declaration

func components(separatedBy separator: String) -> [String]

Parameters

separator

The separator string.

Return Value

An NSArray object containing substrings from the receiver that have been divided by separator.

Discussion

The substrings in the array appear in the order they did in the receiver. Adjacent occurrences of the separator string produce empty strings in the result. Similarly, if the string begins or ends with the separator, the first or last substring, respectively, is empty. For example, this code fragment:

NSString *list = @"Karin, Carrie, David";
NSArray *listItems = [list componentsSeparatedByString:@", "];

produces an array { @"Karin", @"Carrie", @"David"" }.

If list begins with a comma and space—for example, @", Norman, Stanley, Fletcher"—the array has these contents: { "", @"Norman", @"Stanley", @"Fletcher" }

If list has no separators—for example, "Karin"—the array contains the string itself, in this case { @"Karin" }.

See Also

Related Symbols

var pathComponents: [String]

The file-system path components of the receiver.

func componentsJoined(by: String)

Constructs and returns an NSString object that is the result of interposing a given separator between the elements of the array.