As previously stated, a String
is a sequence of Character
elements, and represents plain Text (without any formatting).
In Cuis, as in most programming languages, String
literals are a convenient syntax for creating String
instances. These are some examples of String
literals:
'Hello World!'. 'Accented letters: cigüeña, camión, déjà, deçà, forêt, coïncidence'. 'Non-latin (Greek) letters: Λορεμ ιπσθμ δολορ σιτ αμετ'.
The first example is an instance of String
. This class is used for String literals if all Characters are in the limited ASCII character set. The following examples contain non-ASCII Characters. For these, an instance of UnicodeString
is used instead. You usually don’t need to care about this: Instances of String
and UnicodeString
understand the same messages, so they are interchangeable.
Access to a character in a string is done with the keyword message
#at:
and its index position in the string as the argument. Execute
the following examples with the Ctrl-p shortcut:
'Hello' at: 1 ⇒ $H 'Hello' at: 5 ⇒ $o
Observe how a character is prefixed with the “$” symbol.
Caution. The index indicates position, naturally starting from 1, and is valid up to the string length.
'Hello' indexOf: $e ⇒ 2
To change one character, use the companion two keyword message
#at:put:
. The argument must be noted as a character:
'Hello' at: 2 put: $a; yourself ⇒ 'Hallo'
Observe the use of the cascade with the #yourself
message. A
cascade sends the following messages to the original receiver, so
#yourself
returns the updated string. Without the
cascade, $a
is returned as the result of the #at:put:
message.
Replace each character of the string ’Hello’ to become ’Belle’.
Characters that are part of the ASCII character set can be
asked for their ASCII code. Conversely, given an
ASCII code, we can ask for the corresponding
Character
:
$A asciiValue ⇒ 65 Character asciiValue: 65 + 25 ⇒ $Z
But Characters that are not part of ASCII don’t have an ASCII. In general, it is better to use Unicode code points instead:
$A codePoint ⇒ 65 UnicodeCodePoint codePoint: 65 + 25 ⇒ $Z $φ codePoint ⇒ 966 UnicodeCodePoint codePoint: 966 ⇒ $φ
Shuffling a string can be fun but not very useful. However, shuffling can apply to any kind of collection, not only to a string, and it will prove to be useful as we will see later:
'hello world' shuffled ⇒ 'wod llreohl'
Note that the results of each shuffle are different.
Messages naturally compose:
'hello world' shuffled asArray ⇒ #($h $d $l $ "20" $o $w $e $l $l $o $r)
An Array
literal starts with a hash or sharp character, $#
and parentheses surround the elements of the array.
In this case, the elements are Character
s, but they can
be instances of any class.
Like #shuffled
, all collections answer to the message
#sorted
, which answers a sorted collection.
'hello world' sorted ⇒ ' dehllloorw'