Skip to main content
Engineering LibreTexts

10.4: Strings

  • Page ID
    39629
    \( \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 Hierarchy.
    Figure \(\PageIndex{1}\): The String Hierarchy.

    A String is an indexed Collection that holds only Characters.

    In fact, String is abstract and Pharo 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
    

    Regular expressions will be discussed in more detail in Chapter : Regular Expressions in Pharo.

    Strings support a rather large number of conversion methods. Many of these are shortcut constructor methods for other classes, such as asDate, asInteger 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 : Collections.


    This page titled 10.4: Strings is shared under a CC BY-SA 3.0 license and was authored, remixed, and/or curated by via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.