December 21, 2024
The Protocol of String
String is a subclass of the abstract class CharacterArray. It differs from Array by restricting the kind of elements that can be stored to the class Character.
Collection
SequenceableCollection
ArrayedCollection
Array
CharacterArray
String
ByteString
TwoByteString
Here is an illustration of the string 'Bacon' an instance of the concrete class ByteString
1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|
'B' | 'a' | 'c' | 'o' | 'n' |
The protocol of String supports string manipulation, individual elements can be updated but the size remains constant.
aString := 'Bacon'.
aString at: 2 put: $i;
at: 3 put: $s
will return the string 'Bison', whereas the next line throws an error.
aString at: 6 put: $x
Being a literal, a string can be created by simply writing it, for example, 'John'. It can also be created by this:
myString := String new:4.
myString at: 1 put: $J;
at: 2 put: $o;
at: 3 put: $h;
at: 4 put: $n
This series returns the last argument $n
and myString references 'John'
an instance of ByteString
ByteString is used to store 7 bit ASCII characters
and TwoByteString stores the more exotic eight
bit Unicode characters such as
∑ and ∫.
The fact that an instance of ByteString is returned may seem odd given that String is the receiver to the class message new: 4. What happens is that the new: method answers with an instance of ByteString by sending another message to String called defaultImplementor. This message returns the class ByteString that in turn acts as the receiver to the new: message. You don't really have to concern yourself with the details of this type of thing, but sometimes we all wonder what the hell is going on beneath the surface.
myString indexOf: $J "returns 1"
myString indexOf: $Q "answers 0"
If the character you search for is not in the string, 0 is returned.
What happens if the character is represented in more than one index?
bString := 'Smalltalk'
bString indexOf: $l "answers 4 the first occurence of the character
$l"
bString includes: $l "answers
true"
btring includes: $z "answers
false"
bString size "answers
9"
Some other string messages that we use have been covered earlier.
The concatenate message selector ,
this is in fact a copying message and is inherited from SequenceableCollection.
, aSequenceableCollection
"Answer a copy of the receiver concatenated with the
argument, a
SequenceableCollection."
^self copyReplaceFrom: self size + 1
to: self size
with: aSequenceableCollection
Another useful selector is asNumber which converts a string to a number.
'100' asNumber "answers 100"
Also
bString asUppercase "answers
the string 'SMALLTALK'"
bString asLowercase "answers
the string 'smalltalk'"
bString dropVowels "answers
the string 'Smlltlk'"
Note: bString still references 'Smalltalk', this string has not been modified.
Next page » The protocol of Symbol
Previous page « Scenario using Array
⇑ Up to top of page