Skip to main content
Engineering LibreTexts

8.4: Strings

  • Page ID
    36375
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

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

    The String Hierarchy.
    Figure \(\PageIndex{1}\): The String Hierarchy.

    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.