new line in squeak
Asked Answered
S

4

6

i want to do something like this: Transcript show: '\n'. how?

Switchback answered 19/5, 2009 at 8:58 Comment(0)
F
11

Use the following:

Transcript cr

You can use it after a value via a cascade:

Transcript show: 123; cr
Fungicide answered 19/5, 2009 at 9:5 Comment(0)
C
4

The character itself can be reached as Character cr. So, you could also do this:

Transcript show: 'Bla! , Character cr asString.

But of course,

Transcript show: 'Bla!' ; cr.

is way more elegant.

Coom answered 21/5, 2009 at 12:3 Comment(1)
Character cr asSymbol*, and 'Bla!' <---- close itBowhead
N
4

From my (long) experience, missing character escapes are one of the few things that are missing in Smalltalk. For streaming, solutions using cr, tab etc. are ok.

However, if you need a particular control character in a string, this may be ugly and hard to read (using "streamContents:", or "withCRs" to add a newLine). Alternatively, you may want to use one of the (non-standard) string expansion mechanisms. For example, in VisualWorks or Smalltalk/X, you can write (if I remember correctly):

'someString with newline<n>and<t>tabs' expandMacros

or even with printf-like slicing of other object's printStrings:

'anotherString<n><t>with newlines<n>and<t>tabs and<p>' expandMacrosWith:(Float pi)

I guess, there is something similar in Squeak and V'Age as well.

But, be aware: these expansions are done at execution time. So you may encounter a penalty when heavily using them on many strings.

Nocturn answered 11/5, 2011 at 7:38 Comment(2)
The first one works in Pharo, the second one causes an error, but that may be a bug; still investigating...Oho
The penalty can be mitigated if you express your intention to execute only once with ['someString with newline<n>and<t>tabs' expandMacros] once See When you come BackChecky
P
1

What I do as a convenience is add a method "line" to the String class:

line
    ^self, String lf

Then you can just say obj showSomething: 'Hello world!' line.

Or call it newline, endl, lf, etc...

Prognosis answered 17/4, 2018 at 1:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.