【发布时间】:2019-02-04 12:20:44
【问题描述】:
在 Pharo 7 中,我试图获取字符串的第一个字符数,或者只是整个字符串(如果请求的字符数超过字符串的长度)。
但是,以下示例会导致错误,而我只是希望它返回整个字符串:
'abcdef' copyFrom: 1 to: 30. "=> SubscriptOutOfBounds Error"
'abcdef' first: 30. "=> SubscriptOutOfBounds Error"
'abcdef' first: 3. "=> 'abc'; OK"
如果请求的长度超过字符串长度,是否有一种方法将只返回整个字符串?
作为一种解决方法,我做了以下操作,首先检查字符串的长度,如果长度超过最大长度,只发送first:,但这不是很优雅:
label := aTaskbarItemMorph label size < 30 ifTrue: [ aTaskbarItemMorph label ] ifFalse: [ aTaskbarItemMorph label first: 30 ].
【问题讨论】: