A thousand ways to say new line

From Cuis CookBook
Revision as of 23:39, 3 May 2025 by Nmingott (talk | contribs) (imported material)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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