我正在尝试创建一个带有字符串修剪功能的类:
Object subclass: Trimmer [
trimleading: str [ |ch ret|
ch := (str first: 1). "get first character"
ret := str. "make a copy of sent string"
[ch = ' '] whileTrue: [ "while first char is space"
ret := (ret copyFrom: 2). "copy from 2nd char"
ch := ret first: 1. "again test first char"
].
^ret "return value is modified string"
].
trim: str [ |ret|
ret := str.
ret := (trimleading value: ret). "trim leading spaces"
ret := (trimleading value: (ret reverse)). "reverse string and repeat trim leading"
^(ret reverse) "return reverse string"
]
].
oristr := ' this is a test '
('ORI..>>',oristr,'<<') displayNl.
('FINAL>>',((Trimmer new) trim: oristr),'<<') displayNl.
但是,它没有运行并给出以下错误:
$ gst trimstring_class.st
trimstring_class.st:10: invalid class body element
trimstring_class.st:17: expected expression
问题出在哪里,如何解决?
如果我在整理方法块之后删除了.
,如以下代码所示:
Object subclass: Trimmer [
trimleading: str [ |ch ret flag|
ret := str. "make a copy of sent string"
flag := true.
[flag] whileTrue: [ "while first char is space"
ch := ret first: 1. "again test first char"
ch = ' '
ifTrue: [ ret := (ret copyFrom: 2 to: ret size)] "copy from 2nd char"
ifFalse: [flag := false]
].
^ret "value is modified string"
] "<<<<<<< PERIOD/DOT REMOVED FROM HERE."
trim: str [ |ret|
ret := str.
ret := (trimleading value: ret). "trim leading spaces"
ret := (trimleading value: (ret reverse)). "reverse string and repeat trim leading"
^(ret reverse) "return reverse string"
]
].
然后代码开始运行,但由于以下错误而停止:
$ gst trimstring_class.st
trimstring_class.st:15: undefined variable trimleading referenced
ORI..>> this is a test <<
Object: Trimmer new "<0x7f1c787b4750>" error: did not understand #trim:
MessageNotUnderstood(Exception)>>signal (ExcHandling.st:254)
Trimmer(Object)>>doesNotUnderstand: #trim: (SysExcept.st:1448)
UndefinedObject>>executeStatements (trimstring_class.st:23)
为什么现在trimleading
方法尚未定义,为什么要使用gnu-smalltalk did not understand #trim:
?
答案 0 :(得分:1)
通常,对于这种常见用例,检查是否已实现这种功能是明智的。您可以从中汲取灵感来编写代码(作为Smalltalk程序员,您也将得到改善)。看看sports.st
中的trimBlanksFrom::
SpStringUtilities class >> trimBlanksFrom: aString [
"^a String
I return a copy of aString with all leading and trailing blanks removed."
<category: 'services'>
| first last |
first := 1.
last := aString size.
[last > 0 and: [(aString at: last) isSeparator]]
whileTrue: [last := last - 1].
^last == 0
ifTrue: [String new]
ifFalse: [
[first < last and: [(aString at: first) isSeparator]]
whileTrue: [first := first + 1].
aString copyFrom: first to: last
]
]
如果您只想修剪前导空格,则可以选择第二部分,它是在修剪前导空格。
编辑:OP自身的代码已修复:
Object subclass: Trimmer [
trimleading: str [ |ch ret flag|
ret := str. "make a copy of sent string"
flag := true.
[flag] whileTrue: [ "while first char is space"
ch := ret first: 1. "again test first char"
ch = ' '
ifTrue: [ ret := (ret copyFrom: 2 to: ret size) ] "copy from 2nd char"
ifFalse: [flag := false ]
].
^ret "value is modified string"
] "<<<<<<< PERIOD/DOT REMOVED FROM HERE."
trim: str [ |ret|
ret := str.
ret := self trimleading: (ret copy). "trim leading spaces"
ret := self trimleading: (ret copy reverse). "reverse string and repeat trim leading"
^ (ret reverse) "return reverse string"
]
].
oristr := ' this is a test '
('ORI..>>',oristr,'<<') displayNl.
('FINAL>>',((Trimmer new) trim: oristr),'<<') displayNl.
有一些错误需要纠正。如果要寻址选择器#trimleading:
,则必须使用self
关键字来搜索本地类(查找自己的类或继承的类)。接下来,您不应更改分配给您的变量,而应使用#copy
,否则可能会导致奇怪的结果。