views:

658

answers:

5

Does anybody know what's the newline delimiter for a string in smalltalk?

I'm trying to split a string in separate lines, but I cannot figure out what's the newline character is smalltalk.

ie.

string := 'smalltalk is 
           a lot of fun.
           ok, it's not.'

I need to split it in:
line1: smalltalk is
line2: a lot of fun.
line3: ok, it's not.

I can split a line based on any letter or symbol, but I can't figure out what the newline delimter is.

OK here is how I'm splitting the string based on commas, but I cannot do it based on a new line.

A: 

As noted in this question: Character cr.

Chuck
I tried that earlier but it didn't work. Well actually it kept running forever. I had to kill it.
+1  A: 

A quick solution (I don't know if it is the better) is:

|array | array := mystring findTokens: String cr

Where String cr is the carriage return character

Mariano Martinez Peck
findTokens is not defined. I'm using visual works, I don't know if that would be a problem.
+2  A: 

The newline delimiter is typically the carriage return, i.e., Character cr, or as others mentioned, in a string, String cr. If you wanted to support all standard newline formats, just include both standard delimiters, for example:

string := 'smalltalk is
a lot of fun.'.

string findTokens: String cr, String lf.

Since you now mention you're using VisualWorks, the above won't work unless you have the "squeak-accessing" category loaded (which you probably won't unless you're using Seaside). You could use a regular expression match instead:

'foo
bar' allRegexMatches: '[^', (String with: Character cr), ']+'
Nicholas Riley
I get this error:findTokens is a new message:
So you can also set "sep" to (String with: Character cr) as above. Please mention you're using VW in future, Smalltalk dialects differ substantially as you've discovered.
Nicholas Riley
Thanks a lot, I really appreciate it. String with: Character cr worked perfectly. I will make sure I mention VW in the future. THanks again.
A: 

You can send the String>>withCRs message then delimit the carriage returns with backslashes, thus--

string := 'smalltalk is\ a lot of fun.\ ok, it's not.' withCRs.

mystylplx
(Works in VW and squeak both)
mystylplx
+1  A: 

It is of course depending on the encoding. Could be cr, lf or crlf. For unicode there are a few extra possibilities. See: pharo linesDo:

Stephan Eggermont