是否有一种方法可以从输入行读取,在SmallTalk中逐行读取?我找到了一种方法,即使用" to to:to:carnecr cr"。还有其他方法吗?还是可以将行读为字符串?
预先感谢。
这是
的方式string := 'line one
line two
line three'.
stream := string readStream
现在,
stream nextLine "answers with 'line one'".
stream nextLine "answers with 'line two'".
stream nextLine "answers with 'line three'"
和此时
stream atEnd "answers with true"
请注意,nextLine
在不包含答案的情况下消耗了线路结束。如果最后一行没有线结束,则nextLine
将在末尾停止。
还请注意,这允许循环在stream
具有更多数据
[stream atEnd] whileFalse: [self doSomethingWith: stream nextLine]
如果您想从头开始阅读:
stream reset
,如果您想回到一个previos位置:
stream position: pos
例如
stream nextLine "read first line".
pos2 := stream position "position of the second line".
stream nextLine "read second line".
stream nextLine "read third line".
stream position: pos2 "get back to line 2".
stream nextLine "again, line 2"