Squeak(smalltalk) subSrings 忽略字符串末尾的空字符串

Squeak(smalltalk) subSrings 忽略字符串末尾的空字符串

问题描述:

我正在尝试识别字符串末尾的换行符使用子字符串.

I'm trying to identify the newlines at the end of a string using subStrings.

问题:

check := 's
' subStrings: Character cr asString.

Transcript show: check size ; cr.
"prints: 1"
Transcript show: check ; cr.
"prints: #('s')"

我会期望有 2 个字符串,因为我的检查字符串包含一个 cr 字符,并且 subStrings 返回 #('s''')

I would expect to have 2 strings as my check string contains a cr character, and subStrings to return #('s''')

当检查字符串中存在换行符时

while the newlines are present in the check string

check := 's
'.
Transcript show: check size ; cr.
"prints: 2"

在 Squeak 中,有 splitBy:.它在 SequenceableCollection 中定义.
subStringsfindTokens: 搜索任何一组定界符时,splitBy: 将定界符视为要匹配的整个序列.
而且,这样的分隔符序列总是分割一个左右子集合,可能是空的.

In Squeak, there is splitBy:. It is defined in SequenceableCollection.
While subStrings and findTokens: do search for any of a set of delimiters, splitBy: consider the delimiters as a whole sequence to be matched for.
Also, such delimiter sequence always split a left and right sub-collection, possibly empty.

因此您可以使用:

check := 's
' splitBy: String cr.