Characters, Strings and Symbols Damien Cassou, Stphane Ducasse and - - PowerPoint PPT Presentation

characters strings and symbols
SMART_READER_LITE
LIVE PREVIEW

Characters, Strings and Symbols Damien Cassou, Stphane Ducasse and - - PowerPoint PPT Presentation

Characters, Strings and Symbols Damien Cassou, Stphane Ducasse and Luc Fabresse W6S07 http://www.pharo.org What You Will Learn Characters Strings: a collection of characters Symbols: unique strings W6S07 2 / 15 Characters


slide-1
SLIDE 1

Characters, Strings and Symbols

Damien Cassou, Stéphane Ducasse and Luc Fabresse

W6S07

http://www.pharo.org

slide-2
SLIDE 2

What You Will Learn

Characters Strings: a collection of characters Symbols: unique strings W6S07 2 / 15

slide-3
SLIDE 3

Characters

Characters:

  • $F, $Q $U $E $N $T $i $N

Unprintable characters:

  • Character space, Character tab, Character cr

W6S07 3 / 15

slide-4
SLIDE 4

’Strings’

Delimited by ’ 'eclair au chocolat' 'eclair au chocolat' size > 18 Character space split: 'eclair au chocolat' > an OrderedCollection('eclair' 'au' 'chocolat')

W6S07 4 / 15

slide-5
SLIDE 5

A String: a Collection of Characters

'eclair au chocolat' at: 1 > $e 'eclair au chocolat' do: [:each | Transcript show: each ; cr ]

W6S07 5 / 15

slide-6
SLIDE 6

Quote in Strings

To add a quote in a string with just type it twice

'L''eclair au chocolat'

Pay attention there is only one element

'L''eclair au chocolat' at: 2 > $' 'L''eclair au chocolat' at: 3 > $e

W6S07 6 / 15

slide-7
SLIDE 7

Getting the Last Char

| str | str := 'Tiramisu'. str at: str size > $u str last > $u

W6S07 7 / 15

slide-8
SLIDE 8

Ways to Obtain a String

#mac asString > 'mac' 12 printString >'12' String with: $A

W6S07 8 / 15

slide-9
SLIDE 9

For Concatenation Use ,

'Calvin' , ' & ', 'Hobbes' > 'Calvin & Hobbes'

W6S07 9 / 15

slide-10
SLIDE 10

Take Care With ,

Message comma #, copies strings so multiple concatenations can generate useless intermediate versions 'Calvin' , ' & ', 'Hobbes' > 'Calvin & Hobbes'

Benchmark it If this is worth, use a stream to avoid creating multiple

intermediary strings String streamContents: [ :s | s nextPutAll: 'Calvin'; nextPutAll: ' & '; nextPutAll: 'Hobbes' ]

W6S07 10 / 15

slide-11
SLIDE 11

Symbols

#calvin is a symbol A kind of string Unique in the system Starts with #

  • #class #mac #at:put: #+ #accept:

W6S07 11 / 15

slide-12
SLIDE 12

Symbols are Unique

Two symbols with the same representation points to the same object #calvin == #calvin > true Two strings with the same representation may be different

  • bjects depending on compiler optimisations

W6S07 12 / 15

slide-13
SLIDE 13

Symbols vs. Strings

A symbol is a read-only and unique object A string is a mutable object (for now) Symbols are used as method selectors Symbols are good candidates for identity based

dictionaries (IdentityDictionary)

W6S07 13 / 15

slide-14
SLIDE 14

What You Should Know

Strings are collections of characters Symbols are unique immutable strings W6S07 14 / 15

slide-15
SLIDE 15

A course by and in collaboration with

Inria 2016 Except where otherwise noted, this work is licensed under CC BY-NC-ND 3.0 France https://creativecommons.org/licenses/by-nc-nd/3.0/fr/