A thousand ways to say new line: Difference between revisions

From Cuis CookBook
Jump to navigation Jump to search
(imported material)
 
(hilight)
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>
* first of all remember in Smalltalk you can go newline inside strings literals <code>'hello TYPE-RETURN-HERE '.</code> <syntaxhighlight lang="smalltalk">
* <code>'hello ', (Character lf) asString.</code>
'hello ', (Character lf) asString.
</syntaxhighlight>
* <code>'hello ', (Character newLineCharacter) asString.</code>
*
* <code>'hello ' format: {Character lf. } .</code>
* <syntaxhighlight lang="smalltalk">
* <code>'hello \n' printf: {}.</code>
'hello ', (Character newLineCharacter) asString.
</syntaxhighlight><syntaxhighlight lang="smalltalk">
'hello ' format: {Character lf. } .
</syntaxhighlight>
*
* <syntaxhighlight lang="smalltalk">
'hello \n' printf: {}.
</syntaxhighlight>


=== Using streams ===
=== Using streams ===
Use the #newLine message on streams:
Use the #newLine message on streams:<syntaxhighlight lang="smalltalk">
String streamContents: [:s | s nextPutAll: 'hello'; newLine]
String streamContents: [:s | s nextPutAll: 'hello'; newLine] .
</syntaxhighlight>


=== Let's see how our system encodes newlines characters ===
=== Let's see how our system encodes newlines characters ===

Revision as of 09:50, 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 TYPE-RETURN-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