# 8.4: Strings


The String class is also defined in the category Collections-Strings. A String is an indexed Collection that holds only Characters.

In fact, String is abstract and Squeak Strings are actually instances of the concrete class ByteString.

'hello world' class    →    ByteString


The other important subclass of String is Symbol. The key difference is that there is only ever a single instance of Symbol with a given value. (This is sometimes called “the unique instance property”). In contrast, two separately constructed Strings that happen to contain the same sequence of characters will often be different objects.

'hel','lo' == 'hello'    →    false

('hel','lo') asSymbol == #hello    →    true


Another important difference is that a String is mutable, whereas a Symbol is immutable.

'hello' at: 2 put: $u; yourself → 'hullo'  #hello at: 2 put:$u    →    error


It is easy to forget that since strings are collections, they understand the same messages that other collections do:

#hello indexOf: \$o    →    5


Although String does not inherit from Magnitude, it does support the usual comparing methods, <, = and so on. In addition, String»match: is useful for some basic glob-style pattern-matching:

'*or*' match: 'zorro'    →    true


Should you need more advanced support for regular expressions, there are a number of third party implementations available, such as Vassili Bykov’s Regex package.

Strings support rather a large number of conversion methods. Many of these are shortcut constructor methods for other classes, such as asDate, asFileName and so on. There are also a number of useful methods for converting a string to another string, such as capitalized and translateToLowercase.

For more on strings and collections, see Chapter 9.

This page titled 8.4: Strings is shared under a CC BY-SA 3.0 license and was authored, remixed, and/or curated by Andrew P. Black, Stéphane Ducasse, Oscar Nierstrasz, Damien Pollet via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.