Previous Table of Contents Next


Protocol <ByteArray> Conforms to: <sequencedCollection>

Protocol <Interval> Conforms to: <sequencedReadableCollection>

Strings are indexable collections whose elements are restricted to being character objects. Most of the unique behavior of strings is defined in the protocol <readableString>. One of the important characteristics of strings is that they may be ordered according to an implementation-dependent collating sequence. Therefore, <readableString> conforms to <magnitude> as well as <sequencedReadableCollection>:

Protocol <readableString> Conforms to: <sequencedReadableCollection>, <magnitude>
Messages:
asLowercase ^ <String> Creates a new string with all uppercase letters converted to lowercase
asString ^ <String> Returns an instance of String containing the same characters as the receiver
asSymbol ^ <Symbol> Returns an instance of Symbol containing the same characters as the receiver
asUppercase ^ <String> Creates a new string with all lowercase letters converted to uppercase
match:<readableSring> ^ <boolean> Tests if the string pattern argument matches the receiver
sameAs: <readableString> ^ <boolean> Tests if the receiver and argument are identical except for case differences

The messages asLowercase and asUppercase are used to produce new strings containing the same characters except for case conversions. Except for = and ~=, the magnitude comparison operations (<, <=, >, >=) perform case-insensitive comparisons. The message sameAs: is used to perform an equality test for strings that ignores case differences. The argument to the message match: is a pattern for a simple regular expression pattern matcher. The message returns true or false, depending on whether the receiver matches the pattern. The character # in the pattern matches any single character, and the character * matches any sequence of zero or more characters. Case differences are ignored when matching patterns:

  ‘This Is A Test’ asLowercase
      ‘this is a test’
  ‘this is a test’
      ‘THIS IS A TEST’
  ‘This Is A Test’ = ‘this is a test’
      false
  ‘This Is A Test’ sameAs: ‘this is a test’
      true
  ‘This Is A Test’ match: ‘#his*tes#’
      true

Symbol is a special class of strings that has a single unique instance for each possible sequence of characters. This allows object identity to be used to test whether two symbol values are equal. A special literal syntax is used for to symbols. A symbol literal is a quoted string preceded by a hash symbol (#). Although Smalltalk does not require symbol literals, most implementations also treat message selectors as symbols:

  str1 := String withAll: ‘abc’.
  str2 := String withAll: ‘abc’. “make two distinct   strings”
  str1 == str2
      false
  str1 asSymbol == #’abc’ “a literal symbol”
      true
  str1 asSymbol = str2 asSymbol “identity test performed”
      true

String literals conform to the protocol of <readableString>. Symbol literals and dynamically created symbols conform to <Symbol>, which is a refinement of <readableString>. All other strings (which are all mutable strings) conform to <String>, which is a refinement of both <readableString> and <sequencedCollection>:

Protocol <Symbol> Conforms to: <readableString>

Protocol <String> Conforms to: <readableString> <sequencedCollection>

Arrays, strings, and intervals are indexable but are also fixed size. When one of these objects is created, the number of elements it contains is unchangeable. The remaining types of collections are also indexable, but their sizes may change dynamically. The protocol <sequencedContractibleCollection> defines the behavior for indexable collections from which elements may be removed:

Protocol <sequencedContractibleCollection> Conforms to: <sequencedCollection>
Messages:
removeAtIndex: <integer> ^ <Object> Removes and returns the element at the indexed position
removeFirst ^ <Object> Removes and returns the first element of in receiver
removeFirstIfAbsent: <block0> ^ <Object> Removes and returns the first element of in receiver; evaluates the block if the collection is empty before removing the element
removeLast ^ <Object> Removes and returns the last element of in receiver
removeFirstIfAbsent: <block0> ^ <Object> Removes and returns the last element of in receiver, evaluate the block if the collection is empty before removing the element


Previous Table of Contents Next