From 8eefb90944662f3e36d94796dccc22625a57b55a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arkadiusz=20M=C5=82ynarczyk?= Date: Tue, 24 Aug 2010 16:55:21 +0200 Subject: [PATCH] - implemented componentsSeparatedByCharactersInSet: --- Foundation/CPString.j | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/Foundation/CPString.j b/Foundation/CPString.j index cb5b23917..03ee0521b 100644 --- a/Foundation/CPString.j +++ b/Foundation/CPString.j @@ -268,6 +268,37 @@ var CPStringRegexSpecialCharacters = [ return split(aString); } +/*! + Tokenizes the receiver string using the charactes + in a given set. For example, if the receiver is: + \c "Baku baku to jest skład." + and the set is [CPCharacterSet whitespaceCharacterSet] + the returned array would contain: +
 ["Baku", "baku", "to", "jest", "", "skład."] 
+ Adjacent occurences of the separator characters produce empty strings in the result. + @author Arkadiusz Młynarczyk + @param A character set containing the characters to use to split the receiver. Must not be nil. + @return An CPArray object containing substrings from the receiver that have been divided by characters in separator. +*/ +- (CPArray)componentsSeparatedByCharactersInSet:(CPCharacterSet)separator +{ + CPMutableArray components = [CPMutableArray array]; + + if (separator && self.length) + { + CPRange componentRange = CPMakeRange(0, 0); + for (var i=0; i < self.length; i++) { + if ([separator characterIsMember:self.charAt(i)]) { + componentRange.length = i - componentRange.location; + [components addObject:[self substringWithRange:componentRange]]; + componentRange.location += componentRange.length; + } + }; + + } + return components; +} + /*! Returns a substring starting from the specified index to the end of the receiver. @param anIndex the starting string (inclusive)