A thousand ways to say new line: Difference between revisions

From Cuis CookBook
Jump to navigation Jump to search
(hilight)
No edit summary
 
Line 6: Line 6:
=== A few examples ===
=== A few examples ===


* first of all remember in Smalltalk you can go newline inside strings literals <code>'hello TYPE-RETURN-HERE '.</code> <syntaxhighlight lang="smalltalk">
. first of all remember in Smalltalk you can go newline inside strings literals:
<syntaxhighlight lang="smalltalk">
'hello <PUSH RETURN KEY HERE>'.
</syntaxhighlight>

<syntaxhighlight lang="smalltalk">
'hello ', (Character lf) asString.
'hello ', (Character lf) asString.
</syntaxhighlight>
</syntaxhighlight>

*
* <syntaxhighlight lang="smalltalk">
<syntaxhighlight lang="smalltalk">
'hello ', (Character newLineCharacter) asString.
'hello ', (Character newLineCharacter) asString.
</syntaxhighlight><syntaxhighlight lang="smalltalk">
</syntaxhighlight><syntaxhighlight lang="smalltalk">
'hello ' format: {Character lf. } .
'hello ' format: {Character lf. } .
</syntaxhighlight>
</syntaxhighlight>

*
* <syntaxhighlight lang="smalltalk">
<syntaxhighlight lang="smalltalk">
'hello \n' printf: {}.
'hello \n' printf: {}.
</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 09:52, 5 May 2025

TO-COMPLETE

Problem. You need to add a newline to your string. Dah! This is far more annoying than you may think because there is no universal consensus on what a newline character is. But, for starting we will call newlines the POSIX newlines, since in Unix systems newlines are extremely important.

A few examples

. first of all remember in Smalltalk you can go newline inside strings literals:

'hello <PUSH RETURN KEY HERE>'.
'hello ', (Character lf) asString.
'hello ', (Character newLineCharacter) asString.
'hello ' format: {Character lf. } .
'hello \n' printf: {}.

Using streams

Use the #newLine message on streams:

String streamContents: [:s | s nextPutAll: 'hello'; newLine] .

Let's see how our system encodes newlines characters