Characters, Strings and Symbols

Damien Cassou, St├ęphane Ducasse and Luc Fabresse http://stephane.ducasse.free.fr

What You Will Learn

Characters

'Strings'

Delimited by '

'eclair au chocolat'	
'eclair au chocolat' size
> 18

Character space split: 'eclair au chocolat' 
> an OrderedCollection('eclair' 'au' 'chocolat')	

A String: a Collection of Characters

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

Quote in Strings

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

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

Getting the Last Char

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

str last
> $u

Ways to Obtain a String

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

For Concatenation Use ,

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

Take Care With ,

Message comma #, copies strings so multiple concatenations can generate useless intermediate versions

'Calvin' , ' & ', 'Hobbes'
> 'Calvin & Hobbes'
String streamContents: [ :s |
   s 
     nextPutAll: 'Calvin';
     nextPutAll: ' & ';
     nextPutAll: 'Hobbes' ]

Symbols

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 objects depending on compiler optimisations

Symbols vs. Strings

What You Should Know

/