- Inherits from:
- NSObject
- Package:
- com.apple.yellow.foundation
An NSCharacterSet object represents a set of Unicode characters. String and NSScanner objects use NSCharacterSets to group characters together for searching operations, so that they can find any of a particular set of characters during a search. The cluster's two public classes, NSCharacterSet and NSMutableCharacterSet, declare the programmatic interface for static and dynamic character sets, respectively.
The objects you create using these classes are referred to as character set objects (and when no confusion will result, merely as character sets). Because of the nature of class clusters, character set objects aren't actual instances of the NSCharacterSet or NSMutableCharacterSet classes but of one of their private subclasses. Although a character set object's class is private, its interface is public, as declared by these abstract superclasses, NSCharacterSet and NSMutableCharacterSet.
The NSCharacterSet class declares the programmatic interface for an object that manages a set of Unicode characters (see the NSStringReference class cluster specification for information on Unicode). NSCharacterSet's principal primitive method, characterIsMember, provides the basis for all other instance methods in its interface. A subclass of NSCharacterSet needs only to implement this method for proper behavior. For optimal performance, a subclass should also override bitmapRepresentation which otherwise works by invoking characterIsMember for every possible Unicode value.
NSCharacterSet defines class methods that return commonly used character sets, such as letters (uppercase or lowercase), decimal digits, whitespace, and so on. These "standard" character sets are always immutable, even if created by sending a message to NSMutableCharacterSet. See "Standard Character Sets and Unicode Definitions" below for more information on standard character sets.
You can use a standard character set as a starting point for building a custom set by making a mutable copy of it and changing that. (You can also start from scratch by creating a mutable character set and adding characters to it.)
For performance reasons (explained in "Using a Character Set" ), always finish by converting the working mutable character set into an immutable set. If you need to keep changing the character set after you've created it, of course, you should just use the mutable set.
If your application frequently uses a custom character set, you'll want to save its definition in a resource file and load that instead of explicitly adding individual characters each time you need to create the set. You can save a character set by getting its bitmap representation (an NSData object) and saving that object to a file.
Character set filenames by convention use the extension .bitmap. If you intend for others to use your character set files, you should follow this convention. To read a character set file with a .bitmap extension, simply use the characterSetWithContentsOfFile method.
A character set object doesn't perform any tasks; it simply holds a set of character values to limit operations on strings. The String and NSScanner classes define methods that take NSCharacterSets as arguments to find any of several characters.
Because character sets often participate in performance-critical code, you should be aware of the aspects of their use that can affect the performance of your application. Mutable character sets are generally much more expensive than immutable character sets. They consume more memory and are costly to invert (an operation often performed in scanning a string). Because of this, you should follow these guidelines:
The standard character sets, such as that returned by letterCharacterSet, are formally defined in terms of the normative and informative categories established by the Unicode standard, such as Uppercase Letter, Combining Mark, and so on. The formal definition of a standard character set is in most cases given as one or more of the categories defined in the standard. For example, the set returned by lowercaseLetterCharacterSet include all characters in normative category Lowercase Letters, while the set returned by letterCharacterSet includes the characters in all of the Letter categories.
Note that the definitions of the categories themselves may
change with new versions of the Unicode standard. You can download
the files that define category membership from http://www.unicode.org/
.
- Constructors
- NSCharacterSet
- Creating a standard character set
- alphanumericCharacterSet
- controlCharacterSet
- decimalDigitCharacterSet
- decomposableCharacterSet
- illegalCharacterSet
- letterCharacterSet
- lowercaseLetterCharacterSet
- nonBaseCharacterSet
- punctuationCharacterSet
- uppercaseLetterCharacterSet
- whitespaceAndNewlineCharacterSet
- whitespaceCharacterSet
- Opening a character set file
- characterSetWithContentsOfFile
- Testing set membership
- characterIsMember
- Getting a binary representation
- bitmapRepresentation
- Deriving new character sets
- characterSetByIntersectingCharacterSet
- characterSetByInvertingCharacterSet
- characterSetBySubtractingCharacterSet
- characterSetByUnioningCharacterSet
public NSCharacterSet()
public NSCharacterSet(NSData aData)
public NSCharacterSet(NSRange aRange)
public NSCharacterSet(String aString)
public static NSCharacterSet alphanumericCharacterSet()
See Also: letterCharacterSet, decimalDigitCharacterSet
public static NSCharacterSet characterSetWithContentsOfFile(String aString)
.bitmap
.This method doesn't perform filename-based uniquing of the character sets it creates. To prevent duplication of character sets in memory, cache them and make them available through an API that checks whether the requested set has already been loaded.
public static NSCharacterSet controlCharacterSet()
See Also: illegalCharacterSet
public static NSCharacterSet decimalDigitCharacterSet()
See Also: alphanumericCharacterSet
public static NSCharacterSet decomposableCharacterSet()
Note: This character set doesn't currently include the Hangul characters defined in version 2.0 of the Unicode standard. |
See Also: nonBaseCharacterSet
public static NSCharacterSet illegalCharacterSet()
See Also: controlCharacterSet
public static NSCharacterSet letterCharacterSet()
See Also: alphanumericCharacterSet, lowercaseLetterCharacterSet, uppercaseLetterCharacterSet
public static NSCharacterSet lowercaseLetterCharacterSet()
See Also: uppercaseLetterCharacterSet, letterCharacterSet
public static NSCharacterSet nonBaseCharacterSet()
See Also: decomposableCharacterSet
public static NSCharacterSet punctuationCharacterSet()
public static NSCharacterSet uppercaseLetterCharacterSet()
See Also: lowercaseLetterCharacterSet, letterCharacterSet
public static NSCharacterSet whitespaceAndNewlineCharacterSet()
See Also: whitespaceCharacterSet
public static NSCharacterSet whitespaceCharacterSet()
See Also: whitespaceAndNewlineCharacterSet
public NSData bitmapRepresentation()
A raw bitmap representation of a character set is a byte array of 216 bits (that is, 8192 bytes). The value of the bit at position n represents the presence in the character set of the character with decimal Unicode value n.
public boolean characterIsMember(char aChar)
public NSCharacterSetcharacterSetByIntersectingCharacterSet(NSCharacterSet aCharacterSet)
public NSCharacterSet characterSetByInvertingCharacterSet()
public NSCharacterSet characterSetBySubtractingCharacterSet(NSCharacterSet aCharacterSet)
public NSCharacterSet characterSetByUnioningCharacterSet(NSCharacterSet aCharacterSet)