Smalltalk, newline

Does anyone know what a newline separator is for a string in smalltalk?

I am trying to split a line in separate lines, but I cannot understand what a newline character is in smalltalk.

t

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

I can break a line based on any letter or character, but I cannot understand what a newline separator is.

OK, this is how I split the line based on commas, but I can't do it based on a new line.

+4
source share
5 answers

The shell of a new line is usually a carriage return, i.e. Character cr or, as mentioned in others, in String cr . If you want 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 are now mentioning that you are using VisualWorks, the above will not work unless you have the squeak-accessing category loaded (which probably won't if you are not using Seaside). Instead, you can use a regex:

 'foo bar' allRegexMatches: '[^', (String with: Character cr), ']+' 
+3
source

Quick solution (I don't know how much better it is):

| array | array: = mystring findTokens: String cr

Where String cr is the carriage return character

+1
source

As noted in this question : Character cr .

0
source

You can send a String → withCRs message, then divide the carriage return by a backslash, thus -

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

0
source

This, of course, depends on the encoding. Could be cr, lf or crlf. For Unicode, there are several additional features. See: pharo linesDo:

0
source

All Articles